Why does python -c "..." load different dateutil than python file.py - python

Why would this work:
$ python -c "import dateutil; print dateutil.__version__"
2.6.0
but this would fail on, test.py:
import dateutil; print dateutil.__version__
when run as
$ python test.py
Traceback (most recent call last):
File "test.py ", line 3, in <module>
import dateutil; print dateutil.__version__
AttributeError: 'module' object has no attribute '__version__'
The same python interpreter, same bash session and environment is used for both:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

It turns out I had a dateutil.py file in the same directory. :-(
And . in my PYTHONPATH :-( :-(

Related

pexpect AttributeError: 'module' object has no attribute 'spawn'

What is wrong here?
$ python -V
Python 2.7.5
I have installed pip install pexcept
now when i load it
$ python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pexpect.py", line 10, in <module>
child = pexpect.spawn('ssh %s#%s' % (switch_un, switch_ip))
AttributeError: 'module' object has no attribute 'spawn'
>>>
EDIT
Running Linux CentOS 7.x version (64bit)
As stated HERE pexpect is mainly used for UNIX based operation systems. This error comes up when using windows.

ModuleNotFoundError: No module named 'folium.plugins'; 'folium' is not a package [duplicate]

This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 4 years ago.
I know this question has been asked around quite a few times but none of the solutions have helped me out so far.
I am getting the following error whenever I try running my python script:
M-MBP:folder m$ python3.7 folium.py
Traceback (most recent call last):
File "folium.py", line 3, in <module>
import folium
File "/Users/m/folder/folium.py", line 4, in <module>
from folium.plugins import MarkerCluster
ModuleNotFoundError: No module named 'folium.plugins'; 'folium' is not a package
Notes:
I am running Python3.7, installed via Homebrew;
I've tried installing Folium via pip, conda, and cloning its Git repo directly to my site-packages folder.
None have worked. Any suggestions?
Thanks!
Your script is named the same as the package you want. It's trying to import itself and it doesn't have plugins within it. Name your script something other than folium.py and I believe your problem will disappear.
Demonstrated:
arts#support:~ 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.DEBUG
10
>>>
arts#support:~ 0$ cd tmp
arts#support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/arts/tmp/logging.py", line 5, in <module>
print(logging.DEBUG)
AttributeError: 'module' object has no attribute 'DEBUG'
>>>
arts#support:~/tmp 0$ cat logging.py
import sys
import logging # Imports itself
import os
print(logging.DEBUG)
The reason is, you need to look at sys.path
arts#support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> del sys.path[0]
>>> sys.path
['/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> import logging
>>> logging.DEBUG
10
>>>
arts#support:~/tmp 0$ python3
Python 3.4.9 (default, Aug 14 2018, 21:28:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/arts/tmp/logging.py", line 5, in <module>
print(logging.DEBUG)
AttributeError: 'module' object has no attribute 'DEBUG'
>>>
In this second chunk, you'll see that I deleted the first chunk of the list. This removes your CURRENT DIRECTORY from python's import path, so it now ignored my logging.py file and successfully imported the real logging module.

python3 does not allow urllib import

I installed anaconda for Python 3.6 in MacOs Sierra.
It comes with its own python and it changed the default python to this new one.
Now, I am not able to import anything from urllib.
Here is a snippet of my terminal.
Can someone give some suggestions?
devassy#mydire $ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.request import url2pathname
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/devassy/anaconda3/lib/python3.6/urllib/request.py", line 2585, in <module>
from _scproxy import _get_proxy_settings, _get_proxies
ImportError: dlopen(/Users/devassy/anaconda3/lib/python3.6/lib-dynload/_scproxy.cpython-36m-darwin.so, 2): Symbol not found: _iconv
Referenced from: /usr/lib/libcups.2.dylib
Expected in: /Users/devassy/anaconda3/lib/libiconv.2.dylib
in /usr/lib/libcups.2.dylib
>>>

Utils module not imported

I have downloaded this package of Python Utils , and tried to install it, by entering the folder and typing:
python setup.py install
The output seems fine: the package installed and all dependencies processed.
However, when I try to import:
$ python
Python 2.7.2 (default, Dec 12 2011, 16:10:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named utils
What am I getting wrong?
The package is called python_utils, not utils. import python_utils should work.

Non-reproducible ImportError

I get an ImportError when I run a script that I can't reproduce in a Python interpreter.
$ head -6 bin/taglint
#!/usr/bin/env python
#
import re
from lsaf.lsaf import file_info, error, exit
$ taglint
Traceback (most recent call last):
File "/home/ernest/bin/taglint", line 5, in <module>
from lsaf.lsaf import file_info, error, exit
ImportError: No module named lsaf
However, the import works fine in an interpreter:
$ /usr/bin/env python
Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lsaf.lsaf import file_info, error, exit
>>>
What is happening here?
Additional info:
Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lsaf; print(lsaf)
<module 'lsaf' from '/home/ernest/lib/python/lsaf/__init__.pyc'>
The PYTHONPATH environment variable is set to PYTHONPATH=/home/ernest/lib/python.
It might be an issue with sys.path
check your lsaf
May beYou didn't name your script by lsaf.py
rename it and it will work, your script tries to import itself, but there is no package in lsaf in your own script. importception

Categories