Sometimes we may need to import python files in another path, such as the following directory structure, where we want to import _lstm.py in _train.py in the networks directory and _config.py in the parent directory.
_config.py networks _lstm.py _cnn.py pipelines _train.py
Just two steps
(1) Create an empty __init__.py file in the networks folder
_config.py networks _lstm.py _cnn.py pipelines _train.py
(2) Add a path using the sys library
# _train.py import sys ("..") # This sentence is for importing _config from networks._lstm import * from _config import *
update
Some students have reported that the Python 3.6.7+ method fails. An inelegant workaround is given here, which essentially recursively traverses the file path and adds to the referenced path.
The full test code can be found at/SongDark/Import_Test
# _train.py import sys, os ("..") ([(root, name) for root, dirs, _ in ("../") for name in dirs])
to this article on the import of python import upper directory of the import of the article is introduced to this, more related python import upper directory content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!