two py file:
./src/foo.py
def bar ():
print 'bar!'
./tests/testfoo.py
from foo import bar
print 'testing'
bar ()
in the root folder ‘./’, calling
python ./tests/testfoo.py
the result is
Traceback (most recent call last):
File "./tests/testfoo.py", line 1, in <module>
from foo import bar
ImportError: No module named foo
running the testfoo.py but naturally module foo could not be found by the interpreter. can i give any parameter to interpreter to lookup modules in the folder ‘src’?
Edit #1
also added two empty init.py file.
./src/__init__.py
./tests/__init__.py
still getting the same error.
Edit #2 (Solution)
I solved problem by adding a new initialization py file
prep.py
import os, sys
cur = os.path.dirname (__file__)
pathtest = os.path.join (cur, 'tests')
sys.path.append (pathtest)
then called:
python prep.py tests/testfoo.py
it worked
Try this:
You may need to make
./testsan absolute path…