2

Why does this work fine in python 2.x:

>> import matplotlib
>> import matplotlib.pylab

while this doesn't?

>> import matplotlib as mp
>> import mp.pylab
ImportError: No module named mp.pylab

isn't as just a short hand/alias for the module being used? it doesn't make sense that the first case works and second doesn't. why does it happen?

you can do same with os/path (from @kevin):

>> import os as o
>> import o.path
ImportError: No module named o.path
4
  • 1
    Wild guess: import is dumb and doesn't use variable name lookup on its identifiers the way you might expect. If this is so, I would guess that import matplotlib \n xyz = matplotlib \n import xyz.pylab would also fail, so it's not necessarily the fault of as specifically. Commented Feb 26, 2015 at 19:34
  • its not the local matplotlib you are importing matplotlib.pylab from Commented Feb 26, 2015 at 19:34
  • you are importing matplotlib/pylab.py .. (or perhaps matplotlib/pylab/__init__.py Commented Feb 26, 2015 at 19:35
  • For anyone wishing to experiment with this that doesn't have matplotlib, you can use os and path as replacements for matplotlib and pylab, and get the same result. Commented Feb 26, 2015 at 19:36

1 Answer 1

2

when you say

import foo.bar

you are essentially describing an import file path it will look for foo/bar.py or foo/bar/__init__.py

you could mimic this to see by creating

test.py

x=5

test2.py

import test.x

you will see an error about no module x

this has nothing to do with aliasing imports with as nor does it have anything to do with matplotlib ...

it is what the import statement does, it describes where to find the file

ergo when you type

import mp.pylab

you are telling the filesystem to look for mp/pylab.py or mp/pylab/__init__.py (of coarse neither of those exists)

LARGE DISCLAIMER

this is probably a gross oversimplification

Sign up to request clarification or add additional context in comments.

7 Comments

i never said it has anything to do with matplotlib, i only used it as an example. i'm not sure why you say it has "nothing to do with aliasing imports with as" - this is what causes it. if as is an alias, then it should know that mp.pylab is merely short for matplotlib.pylab and not look for mp/pylab which is silly, rather look for matplotlib.pylab
no it doest ... its not using that at all in an import statement ... an import statement describes a *path* to the file ... you can very easily type mp.some_method_or_attribute and everything works ...
my point is that it's a bug that it's not using the full name before translating the import into a filename path when it's loading the modules. i see your point though that it's the fault of import and not as keyword
its not a bug ... its the way the language was designed
In regards to design, I see it as a kind of rock and a hard place issue. If you treat the identifiers in an import statement as though they were variables, then import os.path would give NameError: name 'os' is not defined because it won't find anything when it does a name lookup for os in order to see if it's an alias. That may be partly why the identifiers in the import statement are treated as literal strings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.