我有一个python3脚本script.py,在其中我要实例化一个在clazz.py中定义的Foobar类。但是,当我尝试导入时,我得到:
$ python3 script.py
...
SystemError: Parent module '' not loaded, cannot perform relative import
这是我的文件结构:
python_import/
├── __init__.py
├── clazz.py
└── script.py
clazz.py:
class Foobar():
def __init__(self):
print("initialized a foobar")
script.py:
from .clazz import Foobar
foobar = Foobar()
它运行很好,如果我摆脱.
的import
; 但是,如果执行此操作,则我的IDE(Intellij IDEA)会以红色下划线标记导入,并且不会自动完成任何操作。我相信.
在python3中包含正确,而Intellij似乎很喜欢,所以除非删除它,否则为什么我的程序无法运行?
我已阅读http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#import,http://python.readthedocs.org/en/latest/reference/import.html,如何在同一目录或子目录中导入类?,Python 3中的相对导入和Python 3中的相对导入不起作用。
我怀疑它可能与virtualenv有关,但是a)我不明白为什么工作目录不会成为PYTHONPATH的一部分,并且b)我不太确定如何在virtualenv中进行更改-Intellij设置了它为我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句