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.
Related
I have a utils file from a GitHub repo and I added some simple functions to it (to unpick and pickle) and the import statement
from utils import existing_module, unpicklefile
returns the following
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: cannot import name 'unpicklefile' from 'utils'
but the other functions import fine, so this isn't a sys.path error.
Help :D
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
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>
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.
I decided to develop my home project in python 3.x rather than 2.x. So I decided to check, if it works under 3.1. I run python3.1 above my package directory and then:
>>> import fathom
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "fathom/__init__.py", line 3, in <module>
from schema import Database
ImportError: No module named schema
when I enter fathom directory however schema can be imported:
>>> import schema
Also when I run python2.6 above my package directory I can do this:
>>> import fathom
My __init__.py has following import:
from schema import Database
from inspectors import PostgresInspector, SqliteInspector, MySqlInspector
Should I add something for python3.1?
Did you try a relative import?
from . import schema
from .inspectors import PostgresInspector
Works in Python 2.6 as well.
The 2to3 script can help you pinpoint more of these problems.