pythonpytestsetuptoolspyproject.toml

How does pytest install pytest and _pytest into site-packages


Within site-packages, there are pytest and _pytest folders. Most of the core code is in _pytest.

When looking at pyproject.toml, I could not find how does setuptools know to install pytest and _pytest.

I thought is it due to the existence of __init__.py files, but other folders like testing/example_scripts have them too, so that's unlikely.

There isn't a setup.py in https://github.dev/pytest-dev/pytest, either. I was looking for something like:

setup(
    name='your_project_name',
    version='0.1',
    packages=find_packages()  # or something like ['pytest', '_pytest']
)

Also, why does pip list and pipdeptree not show _pytest?

I assumed all packages in site-packages will show. I thought packages starting with _ are private like Python's private variables, but there are no options in pip list regarding showing "private packages."


Solution

  • When looking at pyproject.toml, I could not find how does setuptools know to install pytest and _pytest?

    It's internal knowledge in the setuptools code, not expressed in config files. When setuptools builds an sdist or a wheel for pytest it looks into the root directory, finds subdirectory src and knows it's src-layout. So it packs everything under src that looks like packages and subpackages (directories with __init__.py) and modules (*.py). When installing the distribution pip unpacks everything including _pytest.

    Also why does pip list and pipdeptree not show _pytest?

    Because it's not a separate distribution. It's a package (importable directory) distributed inside pytest distribution. pip list and related programs list distributions, not packages.

    I assumed all packages in site-packages will show.

    No, not all packages (importable directories). All installed distributions!

    When installing a distribution pip registers it in its "database" which is a set of *.egg-info and *.dist-info directories under site-packages. Look up a directory pytest-<version>.dist-info and see a file top_level.txt — it lists top-level modules (py means py.py) and packages (both _pytest and pytest) of the distribution. That way it knows what to remove when uninstalling.