Python project structure and relative imports

2019-02-17 23:14发布

问题:

I'm new to Python and I searched google a lot and read some articles about relative imports etc. Despite the fact that I am unable to get it working. Please, consider my following project structure:

/Project
    /docs
    /log
    /prev
    /src
        a.py
    /tests
        /tests1
            b.py
        /tests2
    .gitignore
    README.txt
    program.py

And what I'm trying to achieve is to import a class from file a.py inside of the script b.py. Generally speaking, script b.py should have line with import of a.py. I've read some articles about using __init__.py files, where should I put them? And should I change PYTHONPATH some way, how? And last question, is project structure OK? Thank you for your time and help!

回答1:

You need to use Python unit tests and you can start with how-do-i-run-all-python-unit-tests-in-a-directory.