This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Module location load order is not respected if pkg_resources is imported and a namespace is declared
Type: Stage: resolved
Components: Extension Modules Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: Vadim Kantorov, iritkatriel, r.david.murray
Priority: normal Keywords:

Created on 2015-08-21 10:18 by Vadim Kantorov, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg248948 - (view) Author: Vadim Kantorov (Vadim Kantorov) Date: 2015-08-21 10:18
If module's __init__.py contains "__import__('pkg_resources').declare_namespace(__name__)", the module is not loaded, even though it's located in the current directory and should mask other modules.

Originally I stumbled upon this issue while installing a new version of Google Protobuf. But here's a simpler repro:

$ python -c 'import json; print json.__file__'
/usr/lib/python2.7/json/__init__.pyc

$ mkdir json; echo "print 'test'" > json/__init__.py
$ python -c 'import json; print json.__file__'
test
json/__init__.py

$ echo "__import__('pkg_resources').declare_namespace(__name__); print 'test'" > json/__init__.py
$ python -c 'import json; print json.__file__'
test
/usr/lib/python2.7/json/__init__.pyc

For Protobuf, if you build Python bindings from sources, the google/__init__.py will contain exactly "__import__('pkg_resources').declare_namespace(__name__)" and it prevents the freshly-built module from being loaded, even if the module egg is at the first place in sys.path. So an older version gets loaded and it screws things up.

Ubuntu 14.10, Python 2.7.8
msg248958 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-21 14:55
pkg_resources is 3rd party code (from the point of view of this tracker).  If you are saying its code is revealing a bug in python, can you expand on what exactly the bug is?
msg249047 - (view) Author: Vadim Kantorov (Vadim Kantorov) Date: 2015-08-24 14:09
From my (novice) standpoint, it's very weird that importing pkg_resources in a module's __init__.py causes Python to deviate from its core module import rules / sequence.

Would you consider reporting this issue to pkg_resources's authors more appropriate?
msg249049 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-24 14:36
Yes, unless you can identify a python bug yourself.  I believe pkg_resources does a bunch of imports when it is imported, so I'm not really surprised that it has an effect on the order in which things are imported.
msg249050 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-24 14:38
To be clear: I believe it imports things that are not in the standard library, based on scanning sys.path.  But I haven't studied the code in depth, so I could be wrong.
msg396180 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-20 17:02
2.7 is past EOL and I could not reproduce this on a current python version. Please create a new issue if you are still having a problem with this on 3.9+.

PS C:\Users\User\src\cpython-dev> .\python.bat
Running Release|x64 interpreter...
Python 3.11.0a0 (heads/main-dirty:09eb817115, Jun 20 2021, 16:50:29) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('json/__init__.py', 'w') as f:
...    f.write("__import__('pkg_resources').declare_namespace(__name__); print('test')")
...
70
>>> quit()
PS C:\Users\User\src\cpython-dev> python -c 'import json; print( json.__file__)'
Running Release|x64 interpreter...
test
C:\Users\User\src\cpython-dev\json\__init__.py
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69095
2021-06-20 17:02:21iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg396180

resolution: third party
stage: resolved
2015-08-24 14:38:46r.david.murraysetmessages: + msg249050
2015-08-24 14:36:24r.david.murraysetmessages: + msg249049
2015-08-24 14:09:26Vadim Kantorovsetstatus: pending -> open

messages: + msg249047
2015-08-21 17:38:01brett.cannonsetstatus: open -> pending
2015-08-21 14:55:50r.david.murraysetnosy: + r.david.murray
messages: + msg248958
2015-08-21 10:18:53Vadim Kantorovcreate