I have 10 Python scripts in a single main folder. Some use functions from other scripts. I access everything from a run_main_folder.py file outside. More scripts are to come so for scalability I want to make a new folder inside the main folder to put in scripts. Half of them will stay in place and the other half will go to the new folder, creating a package.
This is the structure before :
run_main_folder.py /main_folder __init__.py script1.py script2.py ... script10.py
This is the new structure :
run_main_folder.py /main_folder __init__.py script1.py script2.py ... script5.py /new_folder __init__.py script6.py script7.py ... script10.py
My question is what do i put in the two __init__.py
files, at the begining of the run_main_folder.py and in each scriptX.py such that :
- inside any scriptX.py files I can access any functions defined other scriptX.py files like so :
script1.func1()
, …,script5.func5()
,new_folder.script6.func6()
, …,new_folder.script10.func10()
; - inside the run_main_folder.py i can access any functions from any scriptX.py files in the same way as above;
I’m also wondering if any of this is good practice.
The directory my Spyder is set on is the one the run_main_folder.py file lives in.
I played arround trying each combinations in the __init__.py
files :
- empty
import main_foler.new_folder
,import main_folder.script1
,…,import main_folder.script5
from main_foler import new_folder
,from main_foler import script1
,…,from main_foler import script5
The last one seems to be the best suited.
I also spent the last 2 days researching this topic.
There is a somewhat simlilar question unanswered : /questions/27091267/best-practice-for-importing-modules-that-contain-imports
This one doesn’t talk about the use of __init__.py
: /questions/50185897/best-practises-for-imports-in-python-3-packages-and-scripts
This one is more focused on the problem with “as” in the import and flies over the stuff inisde __init__.py
: /questions/24807434/imports-in-init-py-and-import-as-statement
Valentin LL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.