matanox
matanox

Reputation: 13686

python 3 project structure for single v.s. multiple modules

In a python project, I have the following directory structure

├── cooccurrence
│   ├── cooccurrence.py
│   ├── __init__.py
├── README.md
└── tests
    ├── __init__.py
    └── test_coccurrence.py

This leads to tests code inside my test source files having a quite ceremonial line:

from cooccurrence.cooccurrence import CoCreate

How would I simplify this overall setup if I only needed a single module, and conversely, what project structure should I have to manage multiple modules under the same package?

To test, I simply use python -m unittest discover -v, and a solution that can also seamlessly enable using the project within PyCharm would be much appreciated.

Upvotes: 1

Views: 269

Answers (2)

Benyamin Jafari
Benyamin Jafari

Reputation: 33946

Put the following code line in cooccurrence/__init__.py path:

from cooccurrence import *

[Note]:

Tested on Python 2.7

Upvotes: 1

luminousmen
luminousmen

Reputation: 2169

You can import files in __init__.py so it will be available on package level. For example you can do in cooccurrence/__init__.py:

from cooccurrence import CoCreate

and then in your test file:

from cooccurrence import CoCreate

It will be the Pythonic way of doing so

Upvotes: 2

Related Questions