Pycharm中的相对进口量

问题描述 投票:1回答:1

一些权威名称在试图在以下模块目录中运行脚本时,对relative imports的一般问题进行了评论和/或回答:Relative imports in Python 3

我的问题是Pycharm IDE 解析模块并在同一目录中运行它。因此,我最终遇到导入错误。请注意,包装中有一个__init__.py。对pycharm解析器看起来不错

enter image description here

from . logger import *   # Logger is a module in same package

但是 ..

enter image description here

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

enter image description here

我尝试将Working Directory放在基本python目录级别:/git/bluej/fusion/python/

enter image description here

但是那没有任何作用。

响应评论:logger不是根包,因此以下代码不起作用:

enter image description here

如何开发,调试和运行在Pycharm中具有相对导入的模块?

python pycharm
1个回答
0
投票

enter image description here

  • 将脚本名称更改为模块名称:请参见Configuration下的第一个选择>>
  • 将工作目录更改为层次结构的根
  • 希望最好
© www.soinside.com 2019 - 2024. All rights reserved.