Strange Python Import Error - python

I am new to Python. I am getting ImportError and seem to have tried everything that's in the documentation and the various notes in this site and other
My code is structured as follows:
vsm
|
|______bin
| vsmx.py
|______site-packages
__init__.py
|
|_____libs
__init__.py
monitor.py
In monitor.py I have a function named getStr and the two __init__.py files are empty
I have the PYTHONPATH set to vsm/site-packages & vsm/site-packages/libs. When I run from command line, python bin/vsmx.py, I get:
Traceback (most recent call last):
File "bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
File "/var/src/vsm/bin/vsmx.py", line 15, in <module>
from libs.monitor import getStr
ImportError: No module named monitor
However, when I try to run this interactively, it seems to work. I tried on both windows and linux using python 2.6.1.
Any pointers will be much appreciated

ImportError: No module... is usually a very (obscure) error meaning that you have circular imports.
Module a.py:
import b
Module b.py:
import a
Then main.py:
import a
This should cause ImportError: No module named a, because a is importing b and not ready when b tries to import it.

Related

Nested `ImportError` on Py3 but not on Py2

I'm having trouble understanding how nested imports work in a python project. For example:
test.py
package/
__init__.py
package.py
subpackage/
__init__.py
test.py:
import package
package/__init__.py:
from .package import functionA
package/package.py:
import subpackage
def functionA():
pass
In Python 3.5 when I run test.py I get the following error, but no error in Python 2.7:
C:\Users\Patrick\Anaconda3\python.exe C:/Users/Patrick/Desktop/importtest/test.py
Traceback (most recent call last):
File "C:/Users/Patrick/Desktop/importtest/test.py", line 1, in <module>
import package
File "C:\Users\Patrick\Desktop\importtest\package\__init__.py", line 1, in <module>
from .package import functionA
File "C:\Users\Patrick\Desktop\importtest\package\package.py", line 1, in <module>
import subpackage
ImportError: No module named 'subpackage'
However if I run package.py with Python 3.5. I get no error at all.
This seems strange to me as when package.py is run on its own the line import subpackage works, but with it is being 'run' (don't know if this is the right terminology here) through the nested import, the same line cannot find subpackage.
Why are there differences between Python 2.7 and 3.5 in this case and how can this be resolved in a way that works for both 2.7.x and 3.x?
I think this might be due to the fact that import subpackage in the nested import counts as an implicit relative import in the nested import but not when package.py is run directly, but if I do import .subpackage instead, I get this error on both 2.7 and 3.5:
C:\Users\Patrick\Anaconda3\python.exe C:/Users/Patrick/Desktop/importtest/test.py
Traceback (most recent call last):
File "C:/Users/Patrick/Desktop/importtest/test.py", line 1, in <module>
import package
File "C:\Users\Patrick\Desktop\importtest\package\__init__.py", line 1, in <module>
from .package import functionA
File "C:\Users\Patrick\Desktop\importtest\package\package.py", line 1
import .subpackage
^
SyntaxError: invalid syntax
You should use:
from . import subpackage
in package/package.py.

package on sys.path, with init importing names, can be used internally, but not from outside it

I have a package my_scripting_library I want to use anywhere on machine. It has an init.py:
from silo_functions import *
and looks like
my_scripting_library
-__init__.py
-silo_functions.py
-test_stuff.py
test_stuff.py looks like:
#!/usr/bin/env python
from silo_functions import *
lines = read_lines('filepath.py')
print lines
in bashrc:
export PYTHONPATH="${PYTHONPATH}:$LIBRARY"
where LIBRARY is a correct filepath to my_scripting_library
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/usr/lib/python2.7/site-packages/lxml-3.3.3-py2.7-linux-x86_64.egg',
'/home/cchilders/scripts/python/my_scripting_library',
...
'/home/cchilders/.ipython']
running test_stuff with from .silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from .silo_functions import *
ValueError: Attempted relative import in non-package
running test_stuff with from my_scripting_library.silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from my_scripting_library.silo_functions import *
ImportError: No module named my_scripting_library.silo_functions
but running test_stuff with from silo_functions import * works:
it prints the lines
Of course I can't use this package from other folders, which is the real issue- I don't want to be forced into throwing all scripts in this one place. This is causing huge problems as I am constantly reusing dozens of functions each script, and over 5 tutorials on making a folder a python package never have worked yet. Why is something on the python path with an init not a package? Thank you
May be it is because you've added '.../python/my_scripting_library' to your path. But there are no 'my_scripting_library.py' at this folder.
If you want to use 'my_scripting_library.silo_functions', try to add '/home/cchilders/scripts/python' (not '/home/cchilders/scripts/python/my_scripting_library') to path.
Because 'my_scripting_library' is module. Python will find this folder, find __init__.py in this folder and mark it as module.

ImportError: No module named util.dtree_util

I get this error when loading a file using cPickle.
Directory tree:
/qanta/preprocess/dparse_to_dtree.py
/qanta/qanta.py
/qanta/util/dtree_util.py
main.py
extract_data.py
main.py imports extract_data.py
extract_data.py imports dparse_to_dtree.py
A function in dparse_to_dtree.py cPickle dumps a dtree object which is defined in dtree_util.py
then from Main.py a subprocess calls qanta.py to execute but there I get the error:
Traceback (most recent call last):
File "qanta/qanta.py", line 142, in <module>
cPickle.load(open(args['data'], 'rb'))
ImportError: No module named util.dtree_util
What is going wrong here?
You also need to add a __init__.py file. See this question.
Not very nice but adding
import sys
sys.path.append('./qanta/util')
and importing with from ... import * solved the problem

'undefined symbol' error when importing C++ module

I have a python script which runs another python script and this latter script imports a module. It fails to do so and returns the following:
Traceback (most recent call last):
File "/some/path/script.py", line 13, in
import Autodock as AD
File "/some/path/to/module/Autodock.py", line 30, in
import BALL
File "/usr/lib/pymodules/python2.7/BALL.py", line 1, in
from BALLCore import *
ImportError: /usr/lib/pymodules/python2.7/BALLCore.so: undefined symbol: _ZN4BALL25FragmentDistanceCollectorclERNS_9CompositeE
However it gets loaded successfully when I just open the python interpreter and enter this:
>>> from BALLCore import *
Other scripts which also run /some/path/to/module/Autodock.py (which is the script importing the module) run successfully. What makes them successfully import the module from the same module-path?
I need to excuse myself for not sharing so much code, because I have no clue where to look. Any guidance would be appreciated.

Why can't I import this Zope component in a Python 2.4 virtualenv?

I'm trying to install Plone 3.3rc4 with plone.app.blob and repoze but nothing I've tried has worked so far. For one attempt I've pip-installed repoze.zope2, Plone, and plone.app.blob into a virtualenv. I have this version of DocumentTemplate in the virtualenv's site-packages directory and I'm trying to get it running in RHEL5.
For some reason when I try to run paster serve etc/zope2.ini in this environment way Python gives the message ImportError: No module named DT_Util? DT_Util.py exists in the directory, __init__.py is there too, and the C module it depends on is there. I suspect there's some circular dependency or failure when importing the C extension. Of course this module would work in a normal Zope install...
>>> import DocumentTemplate
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "DocumentTemplate/__init__.py", line 21, in ?
File ".../lib/python2.4/site-packages/DocumentTemplate/DocumentTemplate.py", line 112, in ?
from DT_String import String, File
File ".../lib/python2.4/site-packages/DocumentTemplate/DT_String.py", line 19, in ?
from DocumentTemplate.DT_Util import ParseError, InstanceDict
ImportError: No module named DT_Util
I must say I doubt DocumentTemplate from Zope will work standalone. You are welcome to try though. :-)
Note that DT_Util imports C extensions:
from DocumentTemplate.cDocumentTemplate import InstanceDict, TemplateDict
from DocumentTemplate.cDocumentTemplate import render_blocks, safe_callable
from DocumentTemplate.cDocumentTemplate import join_unicode
You'll need to make sure those are compiled. My guess is that importing the cDocumentTemplate module fails and thus the import of DT_Util fails.

Categories