Python SystemError: 父模块 '' 未加载,无法执行相对导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33837717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SystemError: Parent module '' not loaded, cannot perform relative import
提问by phez1
I have the following directory:
我有以下目录:
myProgram
└── app
├── __init__.py
├── main.py
└── mymodule.py
mymodule.py:
我的模块.py:
class myclass(object):
def __init__(self):
pass
def myfunc(self):
print("Hello!")
main.py:
主要.py:
from .mymodule import myclass
print("Test")
testclass = myclass()
testclass.myfunc()
But when I run it, then I get this error:
但是当我运行它时,我得到这个错误:
Traceback (most recent call last):
File "D:/Users/Myname/Documents/PycharmProjects/myProgram/app/main.py", line 1, in <module>
from .mymodule import myclass
SystemError: Parent module '' not loaded, cannot perform relative import
This works:
这有效:
from mymodule import myclass
But I get no auto completion when I type this in and there is a message: "unresolved reference: mymodule" and "unresolved reference: myclass". And in my other project, which I am working on, I get the error: "ImportError: No module named 'mymodule'.
但是当我输入它时我没有自动完成并且有一条消息:“未解析的引用:mymodule”和“未解析的引用:myclass”。在我正在处理的另一个项目中,我收到错误消息:“ImportError:没有名为‘mymodule’的模块。
What can I do?
我能做什么?
回答by thomas
I usually use this workaround:
我通常使用这种解决方法:
try:
from .mymodule import myclass
except Exception: #ImportError
from mymodule import myclass
Which means your IDE should pick up the right code location and the python interpreter will manage to run your code.
这意味着您的 IDE 应该选择正确的代码位置,python 解释器将设法运行您的代码。
回答by Erman
I had the same problem and I solved it by using an absolute import instead of a relative one.
我遇到了同样的问题,我通过使用绝对导入而不是相对导入来解决它。
for example in your case, you will write something like this:
例如在你的情况下,你会写这样的东西:
from app.mymodule import myclass
You can see in the documentation.
您可以在文档中看到。
Note that relative imports are based on the name of the current module. Since the name of the main module is always "
__main__
", modules intended for use as the main module of a Python application must always use absolute imports.
请注意,相对导入基于当前模块的名称。由于主模块的名称始终为“
__main__
”,因此用作 Python 应用程序主模块的模块必须始终使用绝对导入。
回答by Ash
If you go one level up in running the script in the command line of your bash shell, the issue will be resolved. To do this, use cd ..
command to change the working directory in which your script will be running. The result should look like this:
如果您在 bash shell 的命令行中运行脚本时上一级,问题将得到解决。为此,请使用cd ..
命令更改脚本将在其中运行的工作目录。结果应如下所示:
[username@localhost myProgram]$
rather than this:
而不是这个:
[username@localhost app]$
Once you are there, instead of running the script in the following format:
一旦你在那里,而不是按以下格式运行脚本:
python3 mymodule.py
Change it to this:
改成这样:
python3 app/mymodule.py
This process can be repeated once again one level up depending on the structure of your Tree diagram. Please also include the compilation command line that is giving you that mentioned error message.
根据树状图的结构,此过程可以在上一级重复一次。还请包括为您提供上述错误消息的编译命令行。
回答by mymusise
if you just run the main.py
under the app
, just import like
如果您只是在main.py
下运行app
,只需导入
from mymodule import myclass
if you want to call main.py
on other folder, use:
如果要调用main.py
其他文件夹,请使用:
from .mymodule import myclass
for example:
例如:
├── app
│?? ├── __init__.py
│?? ├── main.py
│?? ├── mymodule.py
├── __init__.py
└── run.py
main.py
主文件
from .mymodule import myclass
run.py
运行文件
from app import main
print(main.myclass)
So I think the main question of you is how to call app.main
.
所以我认为你的主要问题是如何调用app.main
.