[英]Import from parent folder
我有一個文件夾結構:
├── helper_files
│ ├── CONSTANTS.py
│ ├── bps.py
│ ├── bpsRestPy.py
│ └── helper.py
└── script_files
├── device_cleanup_script.py
├── device_sp_heavy_script.py
├── image_upload_script.py
├── profile_7_script.py
├── profile_9_script.py
├── shut_all_interfaces_script.py
└── test.py
從 test.py 我想訪問 helper.py,但是下面的失敗了。 正確的方法是什么?
import sys
from pathlib import Path
path = str(Path(Path(__file__).parent.absolute()).parent.absolute())
sys.path.insert(0, path)
from helper_files import helper
helper.my_logger()
嘗試:
from ../helper_files import helper
這個對我有用
試試這個,我希望這有效。
import sys
sys.path.append(r'../helper_files') # relative path
import helper
import sys
sys.path.append(r'../')
from helper_files import helper
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.