I have a folder structure like so:
/mylib/
/mylib/__init__.py
/mylib/my_class.py
/mylib/tests/test_my_lib.py
In my test, I have:
from mylib import MyClass
import unittest
I'm getting:
File "test_edgecast_mcc_client.py", line 1, in <module>
from mylib import MyClass
ImportError: No module named mylib
Which, I think, makes sense because the import would be looking inside the tests directory for mylib when it should be looking in ../mylib?
Can anyone share some light on how to get the import to work properly?
asked Jan 17, 2013 at 1:07
doremi
15.4k31 gold badges98 silver badges153 bronze badges
3 Answers 3
I believe your tests package needs an __init__.py file too
answered Jan 17, 2013 at 1:12
dm03514
56.2k18 gold badges117 silver badges147 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Add primary directory to $PYTHONPATH. You can do it from your test_my_lib.py using something like this:
import sys
import os.path
d = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, d)
answered Jan 17, 2013 at 1:16
Yevgen Yampolskiy
7,2583 gold badges28 silver badges23 bronze badges
Comments
try using this from command line and see if that eliminates the error
export PYTHONPATH="$PYTHONPATH:/path/to/mylib/"
answered Jan 17, 2013 at 1:50
Mirage
31.7k64 gold badges173 silver badges266 bronze badges
Comments
lang-py
mylibon your Python module search path?