user1367204
user1367204

Reputation: 4797

Python: What to put in __init__.py files if I'm using several subpackages?

I buily a package with a folder structure like the following:

MyPackage
|
├──mypackage
|     |
|     ├── __init__.py
|     |
|     ├── config.py
|     |
|     ├── data_clean
|     |   ├── __init__.py
|     |   └── f1.py
|     |
|     ├── data_transform
|     |   ├── __init__.py
|     |   └── g1.py
|     |
|     └── stat_calc
|         ├── __init__.py
|         ├── s1.py
|         └── command_line_interface.py # <- users will use this from cmd.exe
| 
├── README.txt    
|
└── setup.py

All 4 __init__.py files are empty. Originally, when I wanted s1.py to use a function in g1.py, I would do

import mypackage.data_transform.g1

and then somewhere down the line I would do

mypackage.data_transform.g1.my_func()

Then, to save, space I changed all of the imports to look like

from mypackage.data_transform.g1 import my_func

and then I would use my_func somewhere. This new syntax is getting a lot of errors, I'm not sure what I should do or if/what I should put stuff into the __init__.py files.

Upvotes: 1

Views: 534

Answers (1)

user1367204
user1367204

Reputation: 4797

In python 3.3+, __init__.py files are not necessary. After I removed all of the __init__.py files from my script, everything worked well with just:

from mypackage.data_transform.g1 import my_func

Upvotes: 1

Related Questions