Sub topic 12
Testing the built package in a clean environment
This chapter assumes you can build an sdist and a wheel with python -m build, and that you know how to create a virtual environment and use pip. Here you install the built wheel somewhere that has never seen your source tree, and check that it works.
Why a clean environment
Running pytest from your project folder proves little about the package you will ship. Your working directory, an editable install, or a stray PYTHONPATH can make imports succeed even when the built wheel is broken. Typical failures that only show up after a clean install:
- A data file or
py.typedthat was never included in the wheel. - A module that exists on disk but is not part of the package.
- A dependency you use but forgot to list in
dependencies. - A console script that points to a function that does not exist.
The src layout helps here, because import greeter cannot succeed from the project root unless the package is installed. A clean environment checks the rest.
Install the wheel in a fresh virtual environment
Build first, then install the wheel into a new environment. Use a path outside your project so nothing leaks in.
python -m build
python -m venv /tmp/verify
/tmp/verify/bin/pip install dist/greeter-0.1.0-py3-none-any.whl
/tmp/verify/bin/greeter AdaOn Windows, the interpreter and scripts live in Scripts\ instead of bin/. The last command should print Hello, Ada!. If it does, the entry point in [project.scripts] resolves and the package imports correctly.
Next, confirm that Python imports the installed copy and not your source tree. Run this from a different directory:
cd /tmp
/tmp/verify/bin/python -c "import greeter; print(greeter.__file__, greeter.__version__)"The path should contain site-packages. The version comes from the installed metadata, so it should match pyproject.toml.
Test the sdist too
Users on platforms without a matching wheel get the sdist, and pip builds a wheel from it. Test that path separately. A missing file in the sdist often breaks this build even when the wheel is fine.
python -m venv /tmp/verify-sdist
/tmp/verify-sdist/bin/pip install dist/greeter-0.1.0.tar.gz
/tmp/verify-sdist/bin/greeter AdaRun your tests against the installed package
Install the test extra and run the suite from the clean environment. Because of the src layout, the tests import the installed package.
/tmp/verify/bin/pip install "greeter[test] @ file://$PWD/dist/greeter-0.1.0-py3-none-any.whl"
/tmp/verify/bin/python -m pytest tests/This also verifies that the test extra declares everything the tests need.
Common mistakes
- Reusing a virtual environment that already has your package or its dependencies installed. Create a new one each time.
- Running the check from the project root. Change directory first so nothing is picked up by accident.
- Installing with
--no-deps, which hides missing dependency declarations.
Exercise
Your wheel installs, but import greeter works only when you run Python from the project root, and fails elsewhere. What does that tell you about your test setup?
Show answer
Python was importing the source tree through the working directory instead of an installed copy. The tests were not exercising the built package. Create a fresh virtual environment, install the wheel, and run the import from a different directory, checking that greeter.__file__ points into site-packages.
Exercise
Why test the sdist in addition to the wheel?
Show answer
pip builds from the sdist when no compatible wheel exists. Files missing from the sdist can break that build even if the wheel you tested works.