How to relate to relative imports in a Python package

I am working on a Python project with approximately the following layout

project/
    foo/
        __init__.py
        useful.py
        test/
            __init__.py
            test_useful.py

test_useful.py tries to import the project.foo.useful file so that it can test it, but it does not work when I say "python project / foo / test / test_useful.py", but it works if I copy it to my current directory and run "python test_useful.py".

What is the correct way to handle this import during development? This does not seem to be a problem after installation, because it will be in PYTHONPATH. Should I use distutils to create an assembly / folder and add it to my PYTHONPATH?

+3
source share
2 answers

, PYTHONPATH "" "". , : -)

:

from project.foo import useful

-, , , , py.test(pip install pytest). , py.test (.. "Py.test foo" , "py.test foo/test/test_useful.py" ). - , .

0

, , distutils/setuptools, Python. (: setup.py, "foo", .)

, Python (, py.test ..), , "foo" . test_useful.py "foo.useful" .

, , , , , Python. .. test/ init.py. , , , , ( , , ).

0

All Articles