gpt4 book ai didi

Python 无法从同一目录导入?

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:03 24 4
gpt4 key购买 nike

我有以下目录结构(我没有写这个所以我假设它必须以某种方式工作?):

tool.py
core/
__init__.py
config.py
common.py

tool.py 间接导入config.pyconfig.py 有一行from common import foo,显示以下错误:

...
File "...\core\config.py", line 5, in <module>
from common import foo
ImportError: No module named 'common'

它可能不相关,但我在 Windows 上使用 Python 3.4,并且 tool.py 目录在系统路径中(我只是将它作为 tool 运行)。

最佳答案

使用相对导入:

from .common import foo

关于Python 无法从同一目录导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29914358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com