When two paths contain the same module name, using sys.path.append(path)
isn't going to change the priority of the imports, since append
puts path
at the end of the python path list
I'd use
sys.path.insert(0,path)
so modules from path
come first
Example: my module foo.py
imports main
and there's one main.py
in the same directory and one main.py
in the sub
directory
foo.py
main.py
sub/main.py
in foo.py if I do:
import sys
sys.path.append("sub")
import main
print(main.__file__)
I get the main.py
file path of the current directory because sys.path
always starts by the main script directory.
So I could remove that directory from sys.path
but it's a bad idea because I could need other modules from there.
Now if I use insert
instead:
import sys
sys.path.insert(0,"sub")
import main
print(main.__file__)
I get sub/main.py
as sub
is the first directory python looks into when searching modules.