Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Chaining sibling directory imports in Python 3.8+

In my Python project, I am having trouble with sibling directory imports. My file structure is as follows:

main_dir/
        __init__.py
        subdir1/
                __init__.py
                module1.py
                module2.py
        subdir2/
                __init__.py
                module3.py
        setup.py

Importantly, module1.py imports all the functions from module2.py, so in module1.py I have the following import:

from module2 import *

There are functions in module1.py that call functions from module2.py within them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Running module1.py obviously works fine. The issue is that I need module3.py to import the functions from module1.py. To be very clear, in module3.py, I need to run a function from module1.py that calls a function from module2.py within it.

In other parts of my project, I have been struggling with importing sibling directories. A fix I found that has worked for Python 3.8.0-3.12.2 is to use a setup.py file and, in files that require a sibling import, use

import sys
sys.path.append('..')
from sibling_dir.module import needed_fun

This has been working, unlike any other solution I have tried such as

from ..sibling_dir.module import *

or from .. import siblingdir.module.

The setup.py file is as follows:

from setuptools import setup, find_packages

setup(name = 'example_name', version = '1.0', packages = find_packages())

(this of course requires the external setuptools package to be installed)

However, back to the main problem. I need module3.py to import functions from module1.py. In module3.py, I have tried the usual setup. Since module1.py imports functions from module2.py, I also import module2.py‘s functions:

In module3.py

import sys
sys.path.append('..')
from subdir1.module2 import *
from subdir1.module1 import *

I have also tried

import sys
sys.path.append('..')
import subdir1.module2
from subdir1.module1 import *

When I try to run module3.py, I get "No module named ‘module2’". This error occurs on the line from subdir1.module1 import *. I am wondering if there is some secret combination of imports to solve this problem, or a different approach entirely. This structure is important to my project, as organization will be very important as it gets larger. Furthermore, relative paths of some kind are essential, I cannot use absolute paths. One final thing that may matter is that none of the __init__.py‘s have anything in them, they are there primarily for setuptools to see those directories as packages (I think). I should also clarify that everything I try I have tried after running a fresh pip install . in the main_dir directory to utilize setuptools.py.

>Solution :

As long as the sibling folders are in a shared package, such as main_dir, you can import directly from there.

from main_dir.subdir1.module1 import *
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading