How to Test an Installed Python Wheel
In this article
When you develop a Python package, testing against your source checkout can hide packaging issues: missing files, incorrect dependencies, or subtle differences in how the code behaves after installation. Testing the actual wheel you plan to distribute ensures that what users will install is what you expect. This guide walks you through creating a clean environment, installing the wheel, and running your test suite against that installed version.
What You'll Need
- A Python environment with
pipandvenvavailable (Python 3.6+). - Your package's source code, including the build configuration (
pyproject.toml,setup.py, orsetup.cfg). - A test suite (e.g., using
pytest,unittest, ornose). - The
buildpackage installed (pip install build) to create the wheel.
Step-by-Step
- Build the wheel – In your project root, run
python -m build --wheel. This creates a.whlfile in thedist/directory. Make sure the build succeeds without errors; if it fails, fix the packaging configuration first. - Create a clean virtual environment – Run
python -m venv test-env. This isolates the test from your development environment, preventing interference from installed packages or your source checkout. - Activate the environment – On Windows:
test-env\Scripts\activate; on macOS/Linux:source test-env/bin/activate. Your shell prompt should change to indicate the environment is active. - Install the wheel – Run
pip install dist/your_package-1.0.0-py3-none-any.whl. Replace the filename with the actual wheel you built. This installs the package exactly as a user would, pulling in any declared dependencies from PyPI. - Verify the installation location – Run
pip show your_packageand note theLocationfield. It should point inside the virtual environment'ssite-packages, not your source directory. Also runpython -c "import your_package; print(your_package.__file__)"to confirm the module is loaded from the installed location. - Run your test suite – Execute your tests from a directory outside your source checkout (e.g.,
cd /tmporcd ~). Usepytest /path/to/your/testsorpython -m unittest discover /path/to/tests. Running from outside the source tree ensures Python doesn't accidentally pick up the local source files instead of the installed package. - Test console scripts (if any) – If your package defines command-line entry points, run them directly (e.g.,
your-command --help). This checks that the scripts are correctly installed and executable. - Check for missing package data – If your package includes non-Python files (templates, data files), verify they are accessible. Write a small script that tries to open a known data file using
importlib.resourcesorpkg_resourcesand run it from the installed environment. - Deactivate and clean up – After testing, run
deactivateto exit the virtual environment. You can delete thetest-envdirectory when done.
Common Mistakes
- Running tests from the source directory – Python adds the current directory to
sys.path, so even with the wheel installed, your tests may import the source version. Always run tests from a neutral directory. - Forgetting to include package data – If your wheel doesn't include necessary data files, tests that rely on them will fail. Use
package-dataorinclude-package-datain your build config. - Not using a clean environment – Reusing your development environment may mask missing dependencies because they're already installed. A fresh venv catches undeclared imports.
- Installing the wheel with
--force-reinstallor--no-depsincorrectly – These flags can skip dependency checks or overwrite existing packages, leading to false results. - Testing an outdated wheel – If you make changes to your source code, rebuild the wheel before testing. Stale wheels give misleading results.
Tips
- Use a CI pipeline – Automate this process on platforms like GitHub Actions or GitLab CI to test wheels on every commit.
- Test on multiple Python versions – Create separate environments for each supported Python version to catch version-specific issues.
- Inspect the wheel contents – Use
unzip -l your_package.whl(orpython -m zipfile -l) to list files inside the wheel. Verify that all expected modules and data files are present. - Use
pip install -e .for development, but always test the wheel before release – Editable installs are convenient, but they don't replicate the installed experience. - Check for dependency conflicts – Run
pip checkafter installing the wheel to ensure all dependencies are satisfied and no conflicts exist.
Troubleshooting
Tests pass in source but fail with the wheel – This usually indicates missing package data or incorrect module paths. Rebuild the wheel after checking your MANIFEST.in and package_data settings. Also, verify that the wheel contains all necessary files by listing its contents.
ImportError: No module named 'your_package' – The wheel may not have installed correctly. Check the install log for errors, and confirm that you're in the active virtual environment. Use pip list to see if the package is listed.
Dependencies not installed – If you used --no-deps when installing, dependencies won't be pulled in. Reinstall without that flag, or manually install the missing dependencies.
Console script not found – Ensure the entry point is correctly defined in pyproject.toml or setup.py. Rebuild the wheel after fixing the configuration, and reinstall.
Tests still import source code – Double-check that you're running tests from a directory that doesn't contain your source. You can also set the environment variable PYTHONPATH to empty to prevent accidental path additions.
Wheel build fails – Read the error message carefully. Common issues include missing setuptools, syntax errors in setup.py, or missing files referenced in the build configuration. Fix these and rebuild.
Frequently Asked Questions
Q: Can I test the wheel without creating a virtual environment?
A: Yes, but it's risky because your global environment may have conflicting packages. A venv ensures a clean, reproducible test.
Q: How do I test a wheel that has platform-specific binaries?
A: Build the wheel on the target platform (or use CI with multiple OS runners). Then test on that same platform, as binary wheels are not portable across operating systems.
Q: What if my tests require a specific version of a dependency that isn't in the wheel's metadata?
A: That's a packaging bug. Add the dependency with the correct version specifier to your project's install requirements, then rebuild the wheel.
Q: Can I test an installed wheel from within the source directory?
A: Technically yes, but it's not recommended because Python may import the source code instead of the installed package. Use a separate directory for testing.
Q: How do I test the wheel with a different Python version?
A: Create a new virtual environment using that specific Python version (e.g., python3.11 -m venv test-env) and install the wheel there. Ensure the wheel is compatible with that Python version.
Q: What is the difference between testing a wheel and testing an sdist?
A: An sdist (source distribution) requires building during installation, which can catch missing build dependencies. A wheel is pre-built, so it tests the actual artifacts users will download. Both are valuable, but they test different aspects of your packaging.