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.
Related
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?
My folder structure is as follows.
main
main.py
library
utils.py
utils.py has the following code
from aws_requests_auth.aws_auth import AWSRequestsAuth
def testAuth():
print('some code code')
main.py has the following code
from library.utils import testAuth
def start():
print ('some code')
I have run pip install aws_requests_auth in both my main and library folder.
When i tried to run python3 main.py I got this error
Traceback (most recent call last):
File "main.py", line 1, in <module>
from library.utils import testAuth
File "/Users/Docs/library/utils.py", line 1, in <module>
from aws_requests_auth.aws_auth import AWSRequestsAuth
ModuleNotFoundError: No module named 'aws_requests_auth'
How do i ensure 3rd party libraries are installed properly for sub folders? Thanks!
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.
I got a strange error while working with Python unittest. I have two folders in my project:
project
code
__init__.py (empty)
app.py (defines my App class)
test
test.py (contains my unit tests)
test.py is:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from code.app import App
class test_func1(unittest.TestCase):
...
When I run test.py I get the message:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...test.py, line 5, in <module>
from code.app import App
ImportError: No module named 'code.app': 'code' is not a package
After verifying that __init__.py was present and banging my head for a while, on a whim I changed the name of the app directory from code to prog:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from prog.app import App
... and everything was suddenly fine. Unittest imported my app properly and ran the tests.
I've searched through https://docs.python.org/3.5/reference/lexical_analysis.html#keywords and https://docs.python.org/3/reference/import.html#path-entry-finders and don't see any indication that code is an illegal directory name. Where would this be documented, and what other directory names are reserved?
System: python 3.4.3 [MSC v1600 32 bit] on win32, Windows 7
code isn't reserved, but it is already defined in the standard library, where is it a regular module and not package. To import from your package, you should use a relative import.
from .code.app import App
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