python import file from different directory 1

python import file from different directory

import sys
# sys.path is a list of absolute path strings
sys.path.append('/path/to/application/app/folder')

import file

Here is what the above code is Doing:
1. It’s adding the absolute path string to the sys.path list.
2. It’s importing the file module.

If you want to add a path relative to the current file, you can use the os module to get the absolute path of the current file and then add it to sys.path.

import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ‘..’)))

import file

Similar Posts