I have several python projects (eg. a, b) that were written independently with each other. I would like to create a third project (c) which uses the routines of these two projects without introducing too many changes to the main contents of (a, b).
Minor changes to the import statements inside (a, b) are ok provided that (a.py, b.py) should still work while called in their respective project folders.
The problem is a bit similar to a previous post (Re-importing different python module with same name) but:
I'm using Python 3 so relative imports seems to work differently.
I would like to know how to replace the line "from utils import *" as shown in the example below.
I have another utils.py under the current working directory shadowing the other utils.py in projects a and b.
For example my project is as follows:
- a
- a.py
- utils.py
- b
- b.py
- utils.py
- c
- main.py
- utils.py
Contents of a/a.py:
import utils
utils.hello()
Contents of a/utils.py:
def hello():
print('hello from a')
Contents of b/b.py:
from utils import *
hello()
Contents of b/utils.py:
def hello():
print('hello from b')
Contents of main.py
import sys
sys.path.append('../a')
import a
sys.path.append('../b')
import b
import utils
utils.hello()
The example prints (I'd like the three lines to print a,b,c respectively):
hello from c
hello from c
hello from c
It looks to me that sys.path.insert
and sys.append
are not very good practice to import modules and could make the project vulnerable to bugs as the project scales up.