Skip to content Skip to sidebar Skip to footer

Python: Import Another Project Module Named Same With A Local Module

I have two python projects. The first one is a django project located at /path/to/project1/ and has below file structure: project1/ |- policy/ |--- models.py And I have another

Solution 1:

You can use the following to achieve your goal:

 project1/
     __init__.py
     policy/
         __init__.py
         models.py (contains SomeModels)

 project2/
     test_file.py
     policy/
         __init__.py

Above is your directory structure. Added is the _init_.py under project1/ so that you can import project1. Now in the test_file.py, you can do the following to achieve your goal:

sys.path.append('/path/to/project1/')
from project1.policy.models import SomeModel

Solution 2:

Change your setup so you can do this:

sys.path.append('/path/to/')
from project1.policy.models import SomeModel
from project2.policy.models import SomeOtherModel

Just adding an empty __init__.py in /path/to/project1 and /path/to/project2 will do.


Post a Comment for "Python: Import Another Project Module Named Same With A Local Module"