Basic python questino about import - python

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>

Related

import from utils import module returns cannot import even though other modules from same file work

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

How can I import functions from other folders in Python project?

I cannot import the functions from other packages in my Python project.
As suggested, I added
sys.path.append(os.path.abspath('../ExportExcel'))
from ExportExcel.export_excel import export_excel
at the beginning of the clip_finder.py.
I also put
sys.path.append(os.path.dirname(__file__)
inside the export_excel.py.
Inside ExportExcel.init.py I put
from .export_excel import export_excel
However I still get this error:
C:\Main\SupportScripts\ClipFinder>python clip_finder.py -h
Traceback (most recent call last):
File "C:\Main\SupportScripts\ClipFinder\clip_finder.py", line 12, in <module>
from ExportExcel.export_excel import export_excel
ModuleNotFoundError: No module named 'ExportExcel'
How can I make it work?

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.

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

Cannot import name _args_from_interpreter_flags

When I try to import multiprocessing in Python 2.7.5 on OS X 10.6.8, I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/__init__.py", line 65, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 40, in <module>
from subprocess import _args_from_interpreter_flags
ImportError: cannot import name _args_from_interpreter_flags
I also tried to install python2.7.6 with homebrew, but this error still occurs.
It sounds like a circular import issue. Try adding this to the the rest of your imports:
from subprocess import _args_from_interpreter_flags
There is a comment above the function in subprocess.py:
# XXX This function is only used by multiprocessing and the test suite,
# but it's here so that it can be imported when Python is compiled without
# threads.
May be related.

Categories