简体   繁体   中英

Python - nested file in one directory importing nested file in another directory

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:

  1. 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.

  2. 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.

  3. 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?

Try these docs: https://docs.python.org/3/tutorial/modules.html#packages

To use your calculate_helpers , inside calculate.py :

  1. add an empty __init__.py into src/ and another into helpers/ (effectively converting them both into packages

  2. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM