0

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
cybertextron
  • 10,547
  • 28
  • 104
  • 208

2 Answers2

2

It can't find the module because you're running the script incorrectly. Run the following in testlib/:

python -m unit.test_ec2
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Yep - that would do it. Also if you don't go a level up you can specify the python path. PYTHONPATH=/home/path/to/testlib/ python unit/test_ec2.py – Rcynic Apr 21 '15 at 19:44
0

Python is not looking for files in directories above yours. Ignacio's answer is correct, and see this for elaboration.

Community
  • 1
  • 1
Ella Sharakanski
  • 2,683
  • 3
  • 27
  • 47