Reputation: 5492
I'm trying to start a Python3 project and I'm having issues with understanding how to set up a project directory so that I can import modules and re-use code. I've already explored countless examples on Stack Overflow and from Google searches but nothing seems to explain this clearly (or maybe I'm just not understanding properly).
Let's say I have the following project directory:
+my_project
|----src
| |---- calculate.py
| |---- distribute.py
|
|----helpers
| |---- calculate_helpers.py
| |---- distribute_helpers.py
|
|----main.py
Given the above project directory, let's say I have the following 3 scenarios:
Within my src/calculate.py
, I want to use a method defined in helpers/calculate_helpers.py
. I tried importing the file via import helpers.calculate_helpers
and from helpers.calculate_helpers import calculate_task
. However, I'm always getting the Module not found
error.
Within my helpers/calculate_helpers.py
, I want to use a method defined in helpers/distribute.py
. This time, I seem to be able to import distribute
and then access the methods defined in that module.
Within my main.py
, I want to use a method defined in src/distribute.py
. If I just have import src.distribute
, this won't work but if I add a __init__.py
file under the src
directory, then import src.distribute
will work. My understanding from reading the docs is that __init__.py
files aren't needed anymore, but I could be misunderstanding.
Can you help me understand why (1) and (3) don't work, why (2) works, and what's the best way in Python to import modules from different nested directories?
Upvotes: 0
Views: 525
Reputation: 9021
Try these docs: https://docs.python.org/3/tutorial/modules.html#packages
To use your calculate_helpers
, inside calculate.py
:
add an empty __init__.py
into src/
and another into helpers/
(effectively converting them both into packages
inside your calculate.py
:
from helpers import calculate_helpers
def some_fun():
calculate_helpers.some_helper()
EDIT 1: - I've not used namespace packages, but maybe this guide with examples will help clarify your third point
Upvotes: 1