Importing modules from parent folder
In Python, you can use the sys.path.append()
method to add the parent directory to the list of paths where Python looks for modules. Here is an example:
import sys
sys.path.append('..')
# Now you can import modules from the parent directory
import my_module
Watch a video course
Python - The Practical Guide
Alternatively, you can use the PYTHONPATH
environment variable to include the parent directory in the Python module search path.
# in the command shell
export PYTHONPATH=..:$PYTHONPATH
# in windows command shell
set PYTHONPATH=..;%PYTHONPATH%
Please be aware that adding a directory to sys.path
will make all modules in that directory importable, not just the one you want to use.
Another method is using importlib.machinery.PathFinder.find_module()
, but that one is more advanced and not recommended for general use.