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: __import__() problem in 3.3
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, brett.cannon, chris.jerdonek, eric.araujo, ncoghlan, pitrou, skrah
Priority: high Keywords:

Created on 2012-07-23 10:18 by skrah, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg166218 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-07-23 10:18
Using b127046831e2, I'm experiencing an import problem during the NumPy build.

I've reduced it to this scenario, which works in 3.2 but not in 3.3.
Note that in NumPy's setup.py, the equivalent of /home/stefan/tmp is 
the first entry in sys.path. I'm not sure if this isolated example is 
terribly useful:


$ pwd
/home/stefan/tmp
$ ls distutils/command/
__init__.py  xyz.py
$ cat distutils/command/__init__.py

distutils_all = ['xyz']
__import__('distutils.command',globals(),locals(),distutils_all)

$ cat distutils/command/xyz.py
$
$ python3.2 distutils/command/__init__.py
$
$ /home/stefan/usr/bin/python3.3 distutils/command/__init__.py
Traceback (most recent call last):
  File "distutils/command/__init__.py", line 3, in <module>
    __import__('distutils.command',globals(),locals(),distutils_all)
ImportError: No module named 'distutils.command.xyz'
$
msg166219 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-07-23 11:02
It looks like distutils/command from the stdlib is searched first
despite the fact that the first path entry is '/home/stefan/tmp'.

If distutils/command is replaced with a/b, the import works:

$ pwd
/home/stefan/tmp
$ 
$ ls a/b/
__init__.py  xyz.py
$ ls distutils/command/
__init__.py  xyz.py
$ 
$ cat a/b/__init__.py 

import sys
sys.path.insert(0, '/home/stefan/tmp')
print(sys.path)

distutils_all = ['xyz']
__import__('a.b',globals(),locals(),distutils_all)


$ cat distutils/command/__init__.py 

import sys
sys.path.insert(0, '/home/stefan/tmp')
print(sys.path)

distutils_all = ['xyz']
__import__('distutils.command',globals(),locals(),distutils_all)



$ /home/stefan/usr/bin/python3.3 a/b/__init__.py 
['/home/stefan/tmp', '/home/stefan/tmp/a/b', '/home/stefan/usr/lib/python33.zip', '/home/stefan/usr/lib/python3.3', '/home/stefan/usr/lib/python3.3/plat-linux', '/home/stefan/usr/lib/python3.3/lib-dynload', '/home/stefan/.local/lib/python3.3/site-packages', '/home/stefan/usr/lib/python3.3/site-packages']
['/home/stefan/tmp', '/home/stefan/tmp', '/home/stefan/tmp/a/b', '/home/stefan/usr/lib/python33.zip', '/home/stefan/usr/lib/python3.3', '/home/stefan/usr/lib/python3.3/plat-linux', '/home/stefan/usr/lib/python3.3/lib-dynload', '/home/stefan/.local/lib/python3.3/site-packages', '/home/stefan/usr/lib/python3.3/site-packages']



$ /home/stefan/usr/bin/python3.3 distutils/command/__init__.py 
['/home/stefan/tmp', '/home/stefan/tmp/distutils/command', '/home/stefan/usr/lib/python33.zip', '/home/stefan/usr/lib/python3.3', '/home/stefan/usr/lib/python3.3/plat-linux', '/home/stefan/usr/lib/python3.3/lib-dynload', '/home/stefan/.local/lib/python3.3/site-packages', '/home/stefan/usr/lib/python3.3/site-packages']
Traceback (most recent call last):
  File "distutils/command/__init__.py", line 7, in <module>
    __import__('distutils.command',globals(),locals(),distutils_all)
ImportError: No module named 'distutils.command.xyz'
msg166222 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-07-23 14:43
Could you explain what is it you’re trying to achieve?
msg166224 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-07-23 14:54
This is not a distutils issue. I want to know why this does not
throw an exception ...

python3.2 distutils/command/__init__.py


... while this raises ImportError:

~/usr/bin/python3.3 distutils/command/__init__.py



For the path structure please see my previous message.
msg166225 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-07-23 15:06
It might be a runpy thing as Nick has been tweaking that lately to deal with pkgutil issues.

And this is of course ignoring the fact that __import__ should never be called directly over importlib.import_module().
msg166230 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-07-23 15:52
Argh. __init__.py was missing in the top directory. For some reason
Python 3.2 does not throw the error. Also, 3.3 does not raise in the
case of the a/b directory structure:

$ tree a
a
`-- b
    |-- __init__.py
    `-- xyz.py

$ ~/usr/bin/python3.3 a/b/__init__.py               
['/home/stefan/tmp', '/home/stefan/tmp/a/b', '/home/stefan/usr/lib/python33.zip', '/home/stefan/usr/lib/python3.3', '/home/stefan/usr/lib/python3.3/plat-linux', '/home/stefan/usr/lib/python3.3/lib-dynload', '/home/stefan/.local/lib/python3.3/site-packages', '/home/stefan/usr/lib/python3.3/site-packages']


With the added __init__.py, also the (fake) distutils package is OK:


$ tree distutils/
distutils/
|-- __init__.py
`-- command
    |-- __init__.py
    `-- xyz.py


$ ~/usr/bin/python3.3 distutils/command/__init__.py
['/home/stefan/tmp', '/home/stefan/tmp/distutils/command', '/home/stefan/usr/lib/python33.zip', '/home/stefan/usr/lib/python3.3', '/home/stefan/usr/lib/python3.3/plat-linux', '/home/stefan/usr/lib/python3.3/lib-dynload', '/home/stefan/.local/lib/python3.3/site-packages', '/home/stefan/usr/lib/python3.3/site-packages']




Brett, if this all looks good to you, this issue can be closed. The
original NumPy build error must be due to something else.

Perhaps 3.3 should raise in the a/b case, too?
msg166238 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012-07-23 16:59
The a/b case is legitimate in Python 3.3; namespace packages are delineated by not defining an __init__.py file. Closing as invalid.
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59639
2012-07-23 16:59:25brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg166238
2012-07-23 15:52:33skrahsetmessages: + msg166230
2012-07-23 15:23:42chris.jerdoneksetnosy: + chris.jerdonek
2012-07-23 15:06:01brett.cannonsetnosy: + ncoghlan
messages: + msg166225
2012-07-23 14:54:16skrahsetmessages: + msg166224
2012-07-23 14:43:50eric.araujosetmessages: + msg166222
2012-07-23 14:36:33brett.cannonsetnosy: + eric.araujo
2012-07-23 11:06:54Arfreversetnosy: + Arfrever
2012-07-23 11:02:12skrahsetmessages: + msg166219
2012-07-23 10:18:03skrahcreate