Sometimes the calling module is no longer the same directory. Direct import is not loaded in. The default load path is the path specified in Sys.path. If you want to specify the directory to be loaded, you need to add this directory to Sys.path.
For example, to load a module in the parent directory's sibling directory.
Current file atm.py the module to be loaded settings.py
1. Find the absolute path of the current file first
Import Osprint (Os.path.abspath (__file__)) #abspath returns the absolute path of the file, the equivalent path of the __file__ file
2. Find the parent directory
Import Osprint (Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) #E: \PYWWW\DAY04\ATM
3. Add Environment variables
Import Osimport Sysbase_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) Sys.path.append (Base_dir) #
5. Loading module
settings.py Code:
def main (): print (' main ')
Python module calls between different directories