Reputation: 349
I would like to know if there is any way to be able to use the modules of a project, regardless of the module's path, if it is in one or more directories above the current script
since what I have tried to do is something like this but it doesn't work.
import os
r = os.path.realpath(file.py)
from r import functionR
When I investigated a little more, I found something that talks about adding the modules to the main path, but I don't know if that is correct?
I hope you can help me a little
added an example route i'm using
root
folder1
folder11
file1.py
folder2
folder22
file2.py
file_one.py
For example, try using the file_one.py module in files that are at lower levels, for example file1.py or file2.py
I hope you can help me
Upvotes: 0
Views: 149
Reputation: 4618
you can try adding the directory containing the modules of interest to the sys.path
import sys
sys.path.extend([put_directory_here])
or add the directory to the PATH environment variable
or add the folder containing the modules to a location that is already in the path i.e. site packages
this way python knows where to look for the modules and they will be accessible anywhere
there's also a ton of info about absolute/relative imports of modules here: Relative imports for the billionth time
Upvotes: 1