[Note] Import of a file in the parent directory in Python
If you want to import a script file located in the parent directory, you can do it as follows. In this example, we are importing the tools.py file located in the parent directory.
>>> import sys,os
>>> sys.path.append(os.pardir)
>>> import tools
- os.pardir is a string constant that represents the parent directory.
- sys.path is a list of strings that indicate the path to search for the file to import. It is initialized with the PYTHONPATH environment variable and the default path at the installation destination. Therefore, if you include the directory of the file you want to import in sys.path, you can import the script file located in the appropriate directory.
Of course, depending on the permission settings, it may not be possible.