繁体   English   中英

尝试从 2 个本地模块导入时,为什么此导入会给我一条错误消息?

[英]Why does this import give me an error message when trying to import from 2 local modules?

我有以下代码结构:

Graph API/
│── main.py
├── helper_functions/
    ├── defines.py
    ├── insights.py

Insights.py 一开始从defines.py导入了2个函数:

from defines import getCreds, makeApiCall

然后它为这个函数使用“makeApiCall”:

def getUserMedia( params ) :
    // Some code about url endpoints etc. 

    return makeApiCall( url, endpointParams, params['debug'] ) # make the api call

我想在 main.py 脚本中使用 getUserMedia 函数,因此我使用以下命令导入它:

from helper_functions.insights import *

但我收到错误:

Traceback (most recent call last):
  File "/Users/Graph_API/main.py", line 1, in <module>
    import helper_functions.insights
  File "/Users/Graph_API/helper_functions/insights.py", line 1, in <module>
    from defines import getCreds, makeApiCall
ModuleNotFoundError: No module named 'defines'

是什么导致了这个错误? 当我在insights.py 中使用getUserMedia 函数时,它工作正常。 我已经尝试将defines.py 导入到main.py 中,但是我得到了同样的错误。

我对编程很陌生,所以我非常感谢你的帮助:)

你应该更换

from defines import getCreds, makeApiCall

from helper_functions.defines import getCreds, makeApiCall

要么

from .defines import getCreds, makeApiCall

您必须从第一个示例中的项目目录提供路径,或者通过添加点从见解文件的相对路径提供路径。

您还可以考虑将init .py 添加到 helper_functions 文件夹中。 在这里结帐: __init__.py 是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM