unit testing - Importing a file from a different directory in Python -
here 2 folder structures:
a/b/c/d/e/f.py a/b/c/g/h/i/test.py
my test.py
wants import class of f.py
. have set root directory c
. tried:
from c.d.f import fclassname
this not working. message is
importerror: no module named c.d.f
i have ensure directories have __init__.py
files. don't want add code test.py
. want add code in __init__.py
applies future test files i'll write.
in __init__.py
of directory h, have written follwing code:
import os import sys root = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../')) sys.path.append(root)
where going wrong?
use file structure inside c
.
. |-- d | |-- e | | |-- f.py | | |-- __init__.py | | `-- __pycache__ | | |-- f.cpython-34.pyc | | `-- __init__.cpython-34.pyc | |-- __init__.py | `-- __pycache__ | `-- __init__.cpython-34.pyc `-- g |-- h | |-- | | `-- foo.py | `-- __init__.py `-- __init__.py
foo.py
be
from d.e import f f.foo()
run pythonpath
including current directory (.
) c
.
$ pythonpath=. python3.4 g/h/i/foo.py
output:
foo
Comments
Post a Comment