4

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!

kubisma1
  • 307
  • 5
  • 13
  • 2
    Have a look at e.g. http://www.jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/ - you should write a `setup.py` and *install* your package for import by the tests. The `/tests` directory itself isn't usually a module (so no `__init__.py`) - see https://pytest.org/latest/goodpractises.html#goodpractises – jonrsharpe Jan 11 '16 at 23:19
  • @jonrsharpe "the /tests directory itself isn't usually a module" - do you mean package rather than module there? – Tom Dalton Jan 11 '16 at 23:54
  • @TomDalton yes, package – jonrsharpe Jan 11 '16 at 23:55
  • 1
    Possible duplicate of [Running unittest with typical test directory structure](http://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure) – Łukasz Rogalski Jan 11 '16 at 23:58

1 Answers1

1

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

Kenly
  • 24,317
  • 7
  • 44
  • 60