簡體   English   中英

從父文件夾導入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM