一些权威名称在试图在以下模块目录中运行脚本时,对relative imports
的一般问题进行了评论和/或回答:Relative imports in Python 3
我的问题是Pycharm
IDE 是解析模块并在同一目录中运行它。因此,我最终遇到导入错误。请注意,包装中有一个__init__.py
。对pycharm
解析器看起来不错
from . logger import * # Logger is a module in same package
但是 ..
Traceback (most recent call last):
File "/git/bluej/fusion/python/pointr/bluej/util/DB.py", line 6, in <module>
from . logger import *
ImportError: attempted relative import with no known parent package
Run Configuration
是通过右键单击模块创建的。自动生成的版本是这样的:working directory
与脚本目录相同。 /git/bluej/fusion/python/pointr/bluej/util
(注意包是pointr.blue.util
):
我尝试将Working Directory
放在基本python
目录级别:/git/bluej/fusion/python/
但是那没有任何作用。
响应评论:logger
不是根包,因此以下代码不起作用:
如何开发,调试和运行在Pycharm
中具有相对导入的模块?