Why does the behavior of find_packages depend on the import of the pip package?

I have the following project structure:

prog
  __init__.py
tests
  subpak
    __init__.py
  __init__.py
run1.py
run2.py

run1.py:

#!/usr/bin/env python3
from setuptools import find_packages


for i in sorted(find_packages(exclude=['tests'])):
    print(i)

run2.py:

#!/usr/bin/env python3
import pip
from setuptools import find_packages


for i in sorted(find_packages(exclude=['tests'])):
    print(i)

The remaining files are empty. Environment - Debian testing. python 3.5.

run1.py output:

$ python3 run1.py 
prog

run2.py output:

$ python3 run2.py 
prog
tests.subpak

That is, when find_packagesimported after pip, it no longer excludes subpackages of the excluded package. Why is this happening and what mechanisms are involved in this peculiar behavior?

edit: It seems that pip or some of its dependencies modify syspath, and the modules are setuptoolsdifferent. Run1:

<module 'setuptools' from '/usr/lib/python3/dist-packages/setuptools/__init__.py'>

RUN2:

<module 'setuptools' from '/usr/share/python-wheels/setuptools-20.10.1-py2.py3-none-any.whl/setuptools/__init__.py'>
+4
source share
1 answer

pip wheel , , , :

<module 'setuptools' from '/usr/share/python-wheels/setuptools-20.10.1-py2.py3-none-any.whl/setuptools/__init__.py'>

setuptools:

<module 'setuptools' from '/usr/lib/python3/dist-packages/setuptools/__init__.py'> 

, , , , test.subpack ( , 'tests.*' exclude).

+1

Source: https://habr.com/ru/post/1658812/


All Articles