I have observed the following behavior in python 3.4.2, and I am unableto explain it. Hopefully someone could shed some light on the matter:
In IPython:
In [129]: import urllib
In [130]: print(urllib.parse)
<module 'urllib.parse' from '/Users/ashwin/.pyenv/versions/3.4.2/lib/python3.4/urllib/parse.py'>
I've imported a module, and printed one of its attributes. Everything works as expected. So far, life is good.
Now, I do the same thing from the command line:
$ python -c 'import urllib; print(urllib.parse)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parse'
Say what?! that's not how that's supposed to work.
Ok, maybe this is a python-wide behavior; maybe modules are not immediately imported when using the -c flag. Let's try another module:
$ python -c 'import datetime; print(datetime.datetime)'
<class 'datetime.datetime'>
What?! How does it work for datetime and not for urllib? I'm using the same version of python in both places (3.4.2)
Does anyone have any thoughts on this?
EDIT:
Per one of the comments:
$ which -a ipython
/Users/ashwin/.pyenv/shims/ipython
/Library/Frameworks/Python.framework/Versions/2.7/bin/ipython
/usr/local/bin/ipython
/usr/local/bin/ipython
And
$ which -a python
/Users/ashwin/.pyenv/shims/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/bin/python
/usr/bin/python
which -a ipythonandwhich -a python- celpython -m IPython -c 'import datetime; print(datetime.datetime)'andpython -c 'import datetime; print(datetime.datetime)'give different results? - celpython -cwithdatetimegives a different result thanpython -cwithurllib, in that the former throws anAttributeErrorand the latter does not. Yet, the the code withurllibdoes not raise and errors when used within the interactive IPython - inspectorG4dgetpython -c 'import urllib; print(urllib.parse)'andpython -m IPython -c 'import urllib; print(urllib.parse)'give different results. I see that too. Indeed a very interesting observation. I would add a regular python tag to increase visibility. - cel