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
Related
I have been trying on setup PIL module on my Ubuntu System and it has been giving a lots of error. Finally , i am getting issue as:
when i try to run python from /home/ubuntu, it gives me error as:
ubuntu#ip-<server>:~$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PIL/Image.py", line 90, in <module>
from . import _imaging as core
ImportError: cannot import name _imaging
but if I run the code from /usr/local, it does not return any error:
ubuntu#ip-<server>:~$ cd /usr/local/
ubuntu#ip-<server>:/usr/local$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>>
there might be an issue with the path or the permission of the required libraries. But i am not sure how to fix this.
I am executing my script from Jenkins and it throws error over there. Please guide how to fix.
Simple example:
File structure is:
test/lib/f1.py
test/lib/f2.py
File contents:
$ cat lib/f1.py
from f2 import bar
def foo(a):
print(1+bar(a))
$ cat lib/f2.py
def bar(a):
return a
So f1.py defines foo, which relies on bar, defined in f2.py.
Now, loading f1.py works just fine when inside test/lib/:
$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from f1 import foo
>>> foo(1)
2
However, when loading from outside test/lib/ (say from test/), this fails with an import error:
$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from lib.f1 import foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/pymod/lib/f1.py", line 1, in <module>
from f2 import bar
ImportError: No module named 'f2'
>>> Quit (core dumped)
Moving the code from f2.py in f1.py fixes the issue, but I don't want to do that.
Why am I getting this error and how can I avoid it?
Define the folder where it should look for the modules:
import sys
sys.path.append('test')
from lib.f1 import foo
I am in need to run the Software Sources GUI (no, my job cannot be completed through apt or aptitude). Since the window would never load if I navigated from the launcher, I decided to see what happens if I were to run it from the command line:
jason#jason-mint:~$ software-sources
File "/usr/lib/linuxmint/mintSources/mintSources.py", line 48
def async(func):
^
SyntaxError: invalid syntax
The function in the file mintSources.py looks like this:
# Used as a decorator to run things in the background
def async(func):
def wrapper(*args, **kwargs):
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
return wrapper
At the very top, the shebang says: #!/usr/bin/python3. Since my default Python is Python 2.7, I changed the shebang to #!/usr/bin/python to see what happens, only to have a new error this time:
jason#jason-mint:/etc/apt$ software-sources
Traceback (most recent call last):
File "/usr/lib/linuxmint/mintSources/mintSources.py", line 6, in <module>
import configparser
ImportError: No module named configparser
Which means it failes in import configparser, clearly an indication that python2.7 (my default) will not work. To clarify, I have both Python2 and Python3 available:
jason#jason-mint:~$ python --version
Python 2.7.16
jason#jason-mint:~$ python2
Python 2.7.16 (default, Apr 6 2019, 01:42:57)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
jason#jason-mint:~$ python3
Python 3.7.3rc1 (default, Mar 13 2019, 11:01:15)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
jason#jason-mint:~$ python
Python 2.7.16 (default, Apr 6 2019, 01:42:57)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Some system info if it helps:
jason#jason-mint:~$ cat /etc/debian_version
buster/sid
jason#jason-mint:~$ uname -r
4.19.0-4-amd64
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.
Isn't log a built in package in python?
# /usr/bin/python
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import log as logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named log
>>>
Do I need to install log with pip ?
logging is a built-in package, not log:
import logging as log