Importing modules from a neighbouring folder in Python
Question:
I have put the question in the figure below:
EDIT
The question put next to the figure is:
How do I make script_A1 import a function from script_B2?
Similar questions have been asked before. But most answers suggest to add the module/script/package(whatever) to the PATH variable. For example:
sys.path.append('...')
But adding the module to the PATH variable just feels so wrong. I do not want to alter my system in any way. When my application closes, I want my Python environment to be clean and ‘untouched’. I’m afraid that adding uncontrolled modules to the PATH variables on my system will cause headaches later on.
Thank you for helping me out 🙂
Answers:
Put this at the top of script_A1;
from folderB.script_B2 import YourClass as your_class
You can use a trick of adding the top folder to path:
import sys
sys.path.append('..')
import folderB.something
You can also use imp.load_source if you prefer.
I think I solved the issue.
In the following way, you can append the parent directory to the PATH. Put this at the top of script_A1
:
import sys
import os
myDir = os.path.dirname(os.path.abspath(__file__))
parentDir = os.path.split(myDir)[0]
if(sys.path.__contains__(parentDir)):
print('parent already in path')
pass
else:
print('parent directory added')
sys.path.append(parentDir)
# Now comes the rest of your script
You can verify that the parent directory myProject
is indeed added to the PATH by printing out:
print(sys.path)
Since the parent directory myProject
is now part of the PATH, you can import scripts/modules/whatever from any of its subdirectories. This is how you import script_B2
from folder_B
:
import folder_B.script_B2 as script_B2
After closing your application, you can verify if your Python environment is restored to its initial state. just print the PATH again and check if the directory you had appended is gone.
As Ronen Ness mentioned, you can use imp to achieve what you want. At the start of script_A1.py:
import imp
file, path, description = imp.find_module("folderB", ["./folderB"])
imp.load_module("folderB", file, path, description)
from folderB.script_B2 import *
I have put the question in the figure below:
EDIT
The question put next to the figure is:
How do I make script_A1 import a function from script_B2?
Similar questions have been asked before. But most answers suggest to add the module/script/package(whatever) to the PATH variable. For example:
sys.path.append('...')
But adding the module to the PATH variable just feels so wrong. I do not want to alter my system in any way. When my application closes, I want my Python environment to be clean and ‘untouched’. I’m afraid that adding uncontrolled modules to the PATH variables on my system will cause headaches later on.
Thank you for helping me out 🙂
Put this at the top of script_A1;
from folderB.script_B2 import YourClass as your_class
You can use a trick of adding the top folder to path:
import sys
sys.path.append('..')
import folderB.something
You can also use imp.load_source if you prefer.
I think I solved the issue.
In the following way, you can append the parent directory to the PATH. Put this at the top of script_A1
:
import sys
import os
myDir = os.path.dirname(os.path.abspath(__file__))
parentDir = os.path.split(myDir)[0]
if(sys.path.__contains__(parentDir)):
print('parent already in path')
pass
else:
print('parent directory added')
sys.path.append(parentDir)
# Now comes the rest of your script
You can verify that the parent directory myProject
is indeed added to the PATH by printing out:
print(sys.path)
Since the parent directory myProject
is now part of the PATH, you can import scripts/modules/whatever from any of its subdirectories. This is how you import script_B2
from folder_B
:
import folder_B.script_B2 as script_B2
After closing your application, you can verify if your Python environment is restored to its initial state. just print the PATH again and check if the directory you had appended is gone.
As Ronen Ness mentioned, you can use imp to achieve what you want. At the start of script_A1.py:
import imp
file, path, description = imp.find_module("folderB", ["./folderB"])
imp.load_module("folderB", file, path, description)
from folderB.script_B2 import *