Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent importing of submodules differs from standard Python3.2 interpreter's behavior #2956

Closed
brannerchinese opened this issue Feb 20, 2013 · 8 comments
Milestone

Comments

@brannerchinese
Copy link

I have a question about Ipython3: importing modules that contain submodules. If I import urllib as U, I can then call U.request.urlopen() and Ipython3 correctly loads the method in question.

But if I use the same commands in the regular python3.2 interpreter, I get an error: "AttributeError: 'module' object has no attribute 'request' ".

It seems Ipython3 is loading submodules, unbidden, while python3.2 does not; that means that code may work on Ipython3 but not at the command line. Is this intentional? Is it documented? I do not see it in the docs, however.

(Environment: Python v. 3.2.3/Ipython 0.13.1 on Ubuntu 12.04 and OS 10.8)

@minrk
Copy link
Member

minrk commented Feb 20, 2013

I think it's just that IPython had already imported urllib.request for its own use before you did.

To check:

In [1]: import sys; print('urllib.request' in sys.modules)
True

and to further check your suspicions for modules in general with a dummy package:

mkdir mymod
touch mymod/__init__.py
echo "foo=5" > mymod/submod.py
ipython
In [1]: import mymod
In [2]: mymod.<TAB>
<no results>
In [3]: import mymod.submod
In [4]: mymod.<TAB>
In [4]: mymod.submod

So I don't think anything untoward is going on.

@brannerchinese
Copy link
Author

Thanks, minrk, for your quick reply.

The only untoward thing is that after the user tests code on Ipython, the same code must be tested on the regular interpreter or command line, lest necessary modules not have been imported correctly. I wonder if there is a way to make this extra step unnecessary.

@minrk
Copy link
Member

minrk commented Feb 20, 2013

I don't think there is anything to do, other than minimize the things IPython imports itself. The fact that one package's imports can affect how modules look to other code in the same process is just an artefact of how Python works.

@takluyver
Copy link
Member

In this specific case, I think it should be fixed by #2868, which defers importing urllib until a method is called that needs it.

@brannerchinese
Copy link
Author

All true, but the fact is that urllib is being treated as a special case for use in Ipython. I can't really see the value of this, as it requires the user to do extra work to enable to same code to run correctly in the standard interpreter.

@takluyver
Copy link
Member

Just checked, this is fixed in master:

In [1]: import urllib as U

In [2]: U.request.urlopen
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-cbeb1d4246db> in <module>()
----> 1 U.request.urlopen

AttributeError: 'module' object has no attribute 'request'

Do you see the issue with any other packages?

@brannerchinese
Copy link
Author

Haven't noticed it elsewhere, but thank you!

@bfroehle
Copy link
Contributor

I agree there isn't much else we can do here. Python doesn't really allow modules to be imported multiple times, so this is (as @minrk pointed out) an artifact of the way IPython and your script share the same Python interpreter.

Shall we close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants