簡體   English   中英

如何使用相對導入從父文件夾中的腳本導入 function

[英]How to use relative import to import a function from a script in the parent folder

如何在腳本中導入 function,其中 function 是在父文件夾中定義的?

在以下文件夾結構中,我想使用

root_folder/
    utils_folder:
        __init__.py
        helper_functions.py (where Function_A is defined)
    module_A_folder:
        Script_A.py (Function_A will be imported and used here)
       

Script_A.py 需要使用 Function_A。

utils_folder__init__.py定義如下:

from .helper_functions import Function_A

當我嘗試像這樣在Script_A.py中導入 Function_A 時:

from ..utils import Function_A

我收到以下錯誤:

ImportError: attempted relative import with no known parent package

我怎樣才能使這項工作? 我使用 Python 3.9 x64。

嘗試:

from utils_folder.helper_functions import Function_A

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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