Python: App can't find a module, regardless of environment - python

An application can't import the "urllib" module from the package "six". But I can import it from a test script in the same environment. Can't figure out why.
❯ nova
Traceback (most recent call last):
File "/usr/bin/nova", line 6, in <module>
from novaclient.shell import main
File "/usr/lib/python2.7/site-packages/novaclient/shell.py", line 45, in <module>
from novaclient import client
File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 34, in <module>
from six.moves.urllib import parse
ImportError: No module named urllib
But if I run a simple test script, it's just fine:
#!/usr/bin/python2
from six.moves.urllib import parse
foo = parse.urlsplit("http://foo.net/florp/blorp?blap=blork")
print foo
Here's the output:
❯ ./test.py
SplitResult(scheme='http', netloc='foo.net', path='/florp/blorp', query='blap=blork', fragment='')
Tried setting my PYTHONPATH explicitly, tried it under virtualenv... nothing..
Any ideas? (Sorry I'm not a Python programmer, so it may be something obvious).

Related

cannot import name 'etree' from 'lxml' in home brew installed package but fine in python shell

I'm trying to run ocrmypdf which was installed via homebrew but am having issues with my local version of lxml (version 4.2.4):
Traceback (most recent call last):
File "/usr/local/bin/ocrmypdf", line 5, in <module>
from ocrmypdf.__main__ import run
File "/usr/local/Cellar/ocrmypdf/9.8.2/libexec/lib/python3.8/site-packages/ocrmypdf/__init__.py", line 18, in <module>
from . import helpers, hocrtransform, leptonica, pdfa, pdfinfo
File "/usr/local/Cellar/ocrmypdf/9.8.2/libexec/lib/python3.8/site-packages/ocrmypdf/pdfa.py", line 38, in <module>
import pikepdf
File "/usr/local/Cellar/ocrmypdf/9.8.2/libexec/lib/python3.8/site-packages/pikepdf/__init__.py", line 54, in <module>
from .models import (
File "/usr/local/Cellar/ocrmypdf/9.8.2/libexec/lib/python3.8/site-packages/pikepdf/models/__init__.py", line 13, in <module>
from .metadata import PdfMetadata
File "/usr/local/Cellar/ocrmypdf/9.8.2/libexec/lib/python3.8/site-packages/pikepdf/models/metadata.py", line 17, in <module>
from lxml import etree
ImportError: cannot import name 'etree' from 'lxml' ($HOME/anaconda3/lib/python3.7/site-packages/lxml/__init__.py)
If I open up a python shell, I'm able to import just fine:
>>> from lxml import etree
>>> etree.__file__
'$HOME/anaconda3/lib/python3.7/site-packages/lxml/etree.cpython-37m-darwin.so'
My pythonpath:
$HOME/anaconda3/lib/python3.7/site-packages:$HOME/Code_Repos/invoice2data//src:$HOME/Code_Repos/invoice2data//src/invoice2data:$HOME/anaconda3/lib/python3.7/site-packages:$HOME/Code_Repos/invoice2data
I'm not sure why the homebrewed package is not picking up the module file even though it says it's pointing to the same site-packages folder.

celery: multiple import as failure

I'm using celery as task runner. It's been working fine with Python 2.7. Recently I upgraded my PC to Ubuntu 16.04 and it starts to give met the following error:
from uuid import UUID, uuid4 as _uuid4, _uuid_generate_random
ImportError: cannot import name _uuid_generate_random
Full call stack:
Traceback (most recent call last):
File "venv/bin/celery", line 7, in <module>
from celery.__main__ import main
File "venv/local/lib/python2.7/site-packages/celery/__init__.py", line 133, in <module>
from celery import five # noqa
File "venv/local/lib/python2.7/site-packages/celery/five.py", line 153, in <module>
from kombu.utils.compat import OrderedDict # noqa
File "venv/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 19, in <module>
from uuid import UUID, uuid4 as _uuid4, _uuid_generate_random
ImportError: cannot import name _uuid_generate_random
This seems rather strange to me. Currently my python version is:
Python 2.7.10
Was there some syntax change within the 2.7.x versions?
_uuid_generate_random has been removed and os.urandom is used instead.
https://hg.python.org/releases/2.7.11/rev/24bdc4940e81
It's a bug in an old version of kombu. Just upgrade and the error should go away.
pip install -U kombu

Python import module works in prompt, but not in script

import weka.core.jvm as jvm
jvm.start()
data_dir = "C:/Data/Python/Weka/Data/"
from weka.core.converters import Loader
loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(data_dir + "logistic.arff")
data.class_is_last()
print(data)
I'm executing the above example code of weka python wrapper from their doc. So I'm sure that there's no problem in the code. All modules are installed. But the code is not working when it is run as script (by pressing F5 in IDLE). It is throwing the following error:
Traceback (most recent call last):
File "C:\Data\Python\Weka\weka.py", line 1, in <module>
import weka.core.jvm as jvm
File "C:\Data\Python\Weka\weka.py", line 1, in <module>
import weka.core.jvm as jvm
ImportError: No module named core.jvm
But the code works when I copy and paste it line by line to the IDLE command prompt. No idea why. Where did I go wrong?
Try changing the name of your file to something that's not the name of any of the module you're importing. For example change weka.py to myscript.py

ImportError: cannot import name MAXREPEAT [duplicate]

I'm running into an issue with cx_Freeze when running a frozen application (works fine unfrozen).
When running the program it results in the following traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec code in m.__dict__
File "PythonApp/mainframe.py", line 3, in <module>
File "/usr/local/lib/python2.7/site-packages/dbus/__init__.py", line 103, in <module>
from dbus._dbus import Bus, SystemBus, SessionBus, StarterBus
File "/usr/local/lib/python2.7/site-packages/dbus/_dbus.py", line 39, in <module>
from dbus.bus import BusConnection
File "/usr/local/lib/python2.7/site-packages/dbus/bus.py", line 39, in <module>
from dbus.connection import Connection
File "/usr/local/lib/python2.7/site-packages/dbus/connection.py", line 27, in <module>
import threading
File "/usr/local/lib/python2.7/threading.py", line 44, in <module>
module='threading', message='sys.exc_clear')
File "/usr/local/lib/python2.7/warnings.py", line 57, in filterwarnings
import re
File "/usr/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
I'm on linux using a version of python 2.7.4 that I built from source, and importing _sre from a prompt works and I can access the MAXREPEAT constant.
This is usually down to cx_Freeze not pulling everything into library.zip and can be fixed by explicitly naming the module in cx_Freezes setup include list and is the solution to this similar question, but that hasn't helped here.
This _sre module seems weird.. there's no _sre file in the library.zip generated but from that error it seems like it can find it, however it can't import that symbol? Surely if the module wasn't there it would be a "No module named _sre" error. Or possibly a circular import but _sre stub doesn't have any imports.
What's odd is I can't seem to find the file either - is this module dynamically created when importing somehow?
find /usr/local/lib/python2.7 -name "_sre*"
doesn't return anything, and the imported _sre module doesn't have a __file__ attribute either, so I've no idea how to make sure it's included as it shows up as a built-in.
>>> import _sre
>>> _sre.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> repr(_sre)
"<module '_sre' (built-in)>"
This is similar to this question also which was asked recently, but in this case he was getting the error in the regular interpreter, however for me it's just in cx_Freeze.
edit
Running python -v does seem like it's a built-in, so I'm not sure why cx_Freeze can miss it, or how I'd fix it.
...
# /usr/local/lib/python2.7/re.pyc matches /usr/local/lib/python2.7/re.py
import re # precompiled from /usr/local/lib/python2.7/re.pyc
# /usr/local/lib/python2.7/sre_compile.pyc matches /usr/local/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /usr/local/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /usr/local/lib/python2.7/sre_parse.pyc matches /usr/local/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /usr/local/lib/python2.7/sre_parse.pyc
...
I encountered this problem when I just upgraded from ubuntu 12.10 to 13.04, and I fixed this by copying the /usr/bin/python to /path/to/my/env/bin/, and it worked just fine
cp /user/bin/python /path/to/my/env/bin/
or, there's a more elegant way to fix this(reference):
mkvirtualenv <existing virtualenv name>
_sre is a built in module, so there's no file to include for it, but it doesn't have a MAXREPEAT attribute in Python 2.7.3:
>>> import _sre
>>> _sre
<module '_sre' (built-in)>
>>> _sre.MAXREPEAT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MAXREPEAT'
My best guess is that your frozen copy somehow has the standard library .py modules from Python 2.7.4, but the compiled Python interpreter from 2.7.3 or an earlier version. I see you're working from /usr/local - maybe it's picking up an older version from /usr.
If all else fails, I got things running using this: http://www.kiwisoft.co.uk/blog/2014/08/17/fixed-importerror-cannot-import-name-maxrepeat
I had the same problem recently. Setting LD_LIBRARY_PATH=
solved the problem.
I was using cx_freeze 4.3.2 on my win 8 machine and it was always showing ImportError: cannot import name MAXREPEAT with cx Freeze if I ever tried to freeze a non built-in module, and once I downloaded version 4.3.1, it works, I'm able to freeze my all python 3.3 programs without any problem now.
I was having similar issues on windows 8 - was just a PYTHONPATH issue. check that PYTHONPATH exists by typing the following into a python session:
import os
os.environ['PYTHONPATH'].split(os.pathsep)
if you get an error set your PYTHONPATH using this approach..
How to add to the pythonpath in windows 7?

ImportError: cannot import name MAXREPEAT with cx_Freeze

I'm running into an issue with cx_Freeze when running a frozen application (works fine unfrozen).
When running the program it results in the following traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec code in m.__dict__
File "PythonApp/mainframe.py", line 3, in <module>
File "/usr/local/lib/python2.7/site-packages/dbus/__init__.py", line 103, in <module>
from dbus._dbus import Bus, SystemBus, SessionBus, StarterBus
File "/usr/local/lib/python2.7/site-packages/dbus/_dbus.py", line 39, in <module>
from dbus.bus import BusConnection
File "/usr/local/lib/python2.7/site-packages/dbus/bus.py", line 39, in <module>
from dbus.connection import Connection
File "/usr/local/lib/python2.7/site-packages/dbus/connection.py", line 27, in <module>
import threading
File "/usr/local/lib/python2.7/threading.py", line 44, in <module>
module='threading', message='sys.exc_clear')
File "/usr/local/lib/python2.7/warnings.py", line 57, in filterwarnings
import re
File "/usr/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
I'm on linux using a version of python 2.7.4 that I built from source, and importing _sre from a prompt works and I can access the MAXREPEAT constant.
This is usually down to cx_Freeze not pulling everything into library.zip and can be fixed by explicitly naming the module in cx_Freezes setup include list and is the solution to this similar question, but that hasn't helped here.
This _sre module seems weird.. there's no _sre file in the library.zip generated but from that error it seems like it can find it, however it can't import that symbol? Surely if the module wasn't there it would be a "No module named _sre" error. Or possibly a circular import but _sre stub doesn't have any imports.
What's odd is I can't seem to find the file either - is this module dynamically created when importing somehow?
find /usr/local/lib/python2.7 -name "_sre*"
doesn't return anything, and the imported _sre module doesn't have a __file__ attribute either, so I've no idea how to make sure it's included as it shows up as a built-in.
>>> import _sre
>>> _sre.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> repr(_sre)
"<module '_sre' (built-in)>"
This is similar to this question also which was asked recently, but in this case he was getting the error in the regular interpreter, however for me it's just in cx_Freeze.
edit
Running python -v does seem like it's a built-in, so I'm not sure why cx_Freeze can miss it, or how I'd fix it.
...
# /usr/local/lib/python2.7/re.pyc matches /usr/local/lib/python2.7/re.py
import re # precompiled from /usr/local/lib/python2.7/re.pyc
# /usr/local/lib/python2.7/sre_compile.pyc matches /usr/local/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /usr/local/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /usr/local/lib/python2.7/sre_parse.pyc matches /usr/local/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /usr/local/lib/python2.7/sre_parse.pyc
...
I encountered this problem when I just upgraded from ubuntu 12.10 to 13.04, and I fixed this by copying the /usr/bin/python to /path/to/my/env/bin/, and it worked just fine
cp /user/bin/python /path/to/my/env/bin/
or, there's a more elegant way to fix this(reference):
mkvirtualenv <existing virtualenv name>
_sre is a built in module, so there's no file to include for it, but it doesn't have a MAXREPEAT attribute in Python 2.7.3:
>>> import _sre
>>> _sre
<module '_sre' (built-in)>
>>> _sre.MAXREPEAT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MAXREPEAT'
My best guess is that your frozen copy somehow has the standard library .py modules from Python 2.7.4, but the compiled Python interpreter from 2.7.3 or an earlier version. I see you're working from /usr/local - maybe it's picking up an older version from /usr.
If all else fails, I got things running using this: http://www.kiwisoft.co.uk/blog/2014/08/17/fixed-importerror-cannot-import-name-maxrepeat
I had the same problem recently. Setting LD_LIBRARY_PATH=
solved the problem.
I was using cx_freeze 4.3.2 on my win 8 machine and it was always showing ImportError: cannot import name MAXREPEAT with cx Freeze if I ever tried to freeze a non built-in module, and once I downloaded version 4.3.1, it works, I'm able to freeze my all python 3.3 programs without any problem now.
I was having similar issues on windows 8 - was just a PYTHONPATH issue. check that PYTHONPATH exists by typing the following into a python session:
import os
os.environ['PYTHONPATH'].split(os.pathsep)
if you get an error set your PYTHONPATH using this approach..
How to add to the pythonpath in windows 7?

Categories

Resources