简体   繁体   English

从父文件夹导入

[英]Import from parent folder

I have a folder structure as:我有一个文件夹结构:

├── 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

From test.py I want to access the helper.py, but the below one fails.从 test.py 我想访问 helper.py,但是下面的失败了。 Whats the correct way?正确的方法是什么?

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()

try:尝试:

from ../helper_files import helper

it works for me这个对我有用

Try this I hope this works.试试这个,我希望这有效。

import sys
sys.path.append(r'../helper_files') # relative path 

import helper

OR YOU COULD CHANGE THIS TO或者您可以将其更改为

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