Python error - “ImportError: No module named” - python

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.

Related

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?

How to run a file in pycharm?

I have a project that I imported into pycharm. It looks like this:
The file grabber.py references class Fetcher within fetcher.py like this:
from grabber.fetcher import Fetcher
from the root folder Automaton I can run grabber.py from the command line with this command:
python -m grabber.grabber
But, I want to run grabber.py from pycharm. When I try I get this error:
Traceback (most recent call last):
File "C:/Automaton/grabber/grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
File "C:\Automaton\grabber\grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
ImportError: No module named 'grabber.fetcher'; 'grabber' is not a package
How do I get pycharm to run the file the same way I can from the command line?
My problem was pretty silly. I was having the problem because I had a package grabber and a file within it called grabber.py That name collision caused the problem. I renamed grabber.py and it works.

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

importing a file as a package - Python

I have the following directory structure:
/testlib
__init__.py
ec2.py
unit/
__init__.py
test_ec2.py
utils/
__init__.py
I'm trying to create a unittest class for ec2.py:
import ec2
class TestEC2(unittest.TestCase):
def setUp(self):
self.ec2obj = ec2.EC2(name="testlib_unittest")
if __name__ == '__main__':
unittest.main()
However, when I execute test_ec2.py I'm getting the following error:
python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import ec2
ImportError: No module named ec2
I still don't understand why I'm getting that, since I have the __init__.py properly set in the directory. The __init__.py files are completely empty: I've created them with touch __init__.py. Any clues?
**** Updates ****
Here are some suggestions:
/testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
from ..ec2 import EC2
ValueError: Attempted relative import in non-package
testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import testlib.ec2 as ec2
ImportError: No module named testlib.ec2
It can't find the module because you're running the script incorrectly. Run the following in testlib/:
python -m unit.test_ec2
Python is not looking for files in directories above yours. Ignacio's answer is correct, and see this for elaboration.

ImportError: No module named lines

from lines import lines
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import lines
ImportError: No module named lines
This is taken from this example for Pycha. How I can install this Module?
The module lines.py is part of the Pycha examples. You can find a copy of that module in that directory.
To follow the example correctly, you should call the examples/barchart.py example while still maintaining the rest of the examples directory. Then it will work.
If you want to create an own script based on the example, you’ll have to replace that module with your own code/data.
The module should be in a file name lines.py that is somewhere in your module search-path.

Categories