Importing files from different folder
To import a module from a different folder in Python, you can use the sys.path.append()
function to add the path to the folder containing the module to your Python path.
Here's an example:
import sys
sys.path.append('/path/to/folder')
import module
You can also use the PYTHONPATH
environment variable to specify directories that should be searched for modules. To do this, you can set the PYTHONPATH
environment variable to a list of directories separated by :
on Unix-like systems (e.g., Linux, macOS), or ;
on Windows.
For example, to set the PYTHONPATH
variable on Unix-like systems:
export PYTHONPATH=$PYTHONPATH:/path/to/folder
Or, on Windows:
set PYTHONPATH=%PYTHONPATH%;C:\path\to\folder
Then, you can import the module using import module
as usual.
Alternatively, you can use the -m
flag when running a Python script to specify the module you want to import. For example:
python -m module
This will import the module as if you had written import module
at the top of your script.