ImportError: No module named util.dtree_util - python

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

Related

Python not finding module in same folder when called from a script in another folder

I have a set of Python scripts that interact. I call my "main" script from the parent directory (parent/main.py), which imports function from a script foo in a subdirectory (parent/child/foo.py), which in turn imports functions from another program bar (parent/child/bar.py). I'm getting a ModuleNotFoundError for bar.py when I try to run main.py.
I read into namespace modules and as best I can tell, I'm not doing anything wrong as apparently "from child.foo import myfunc" should be fine?
Stack trace, per request:
Traceback (most recent call last):
File ".\main.py", line 2, in <module>
import child.foo
File "C:\biglongpath\parent\child\run_n1.py", line 1, in <module>
from bar import myfunc as mf
ModuleNotFoundError: No module named 'bar'
In Python 3.x you need to write:
from child.foo import myfunc
In Python 2.7:
You should include an __init__.py file (even if it is empty) in each child directory.

Python error - “ImportError: No module named”

First of all this is what my directory looks like:
.:
ref.py main.py utility
./utility:
file_manip.py
When I execute main.py I get this error:
>>> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import utility.file_manip as fm
ImportError: No module named utility.file_manip
I cannot for the life of me get to the bottom of this. Clearly utility.file_manip exists...
You need a blank __init__.py file in your utility directory in order for Python to see it as a package. See the tutorial.

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.

Basic python questino about import

I am trying to run some Python (2.7.6) code, and the compiler can't find the "util" module.
Here is the line from the code:
from util import getArgs, write
And the error is this:
Traceback (most recent call last):
File "rs.py", line 11, in
from util import getArgs, write
ImportError: No module named util
I can't find this util module anywhere, and pip install does not recognize util, getArgs, and write. How can I fix this problem?
As some of the comments have pointed out, it should be utils (plural) and not util
from utils import <your package>

Strange Python Import Error

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.

Categories