父模块''未加载,无法执行相对导入

时间:2021-03-15 13:13:28

This is the project structure.

这是项目结构。

--KP
   --app
       --api
           --views
                --mpg.py
                --gtt.py

mpg.py:

def cmcid():
     .....
     .....

gtt.py:

from .mpg import cmcid

def main():
    variable = cmcid()
if __name__ == "__main__":
    main()

when I run from views directory, i.e kp/app/api/views$python gtt.py

当我从views目录运行时,即kp / app / api / views $ python gtt.py

I get an error as:

我得到一个错误:

Parent module '' not loaded, cannot perform relative import

父模块''未加载,无法执行相对导入

Any help is appreciated. Thanks..

任何帮助表示赞赏。谢谢..

1 个解决方案

#1


0  

You probably miss a __init__.py file (it can be just an empty file) in your views directory.

您可能会在views目录中遗漏__init__.py文件(它可能只是一个空文件)。

Add this file and it will turn your directory into a python package and the import should work

添加此文件,它将您的目录转换为python包,导入应该工作

Note that it is also required in parent directories.

请注意,父目录中也需要它。


Another possible cause is that you use the Django "machinery" in your file, as a consequence it may be not possible to run directly as a python file.

另一个可能的原因是你在文件中使用Django“机器”,因此可能无法直接作为python文件运行。

If you need to write python scripts, that interacts with your django app, you need to create a django command that will be executed with python manage.py got

如果你需要编写与你的django应用程序交互的python脚本,你需要创建一个django命令,该命令将使用python manage.py获得

Your gtt.py file needs to be in management\commands submodule of your django app.

您的gtt.py文件需要位于django应用程序的management \ commands子模块中。

It must have the following structure

它必须具有以下结构

from django.core.management.base import BaseCommand
from path.to.mpg import cmcid

class Command(BaseCommand):
    help = 'Your custom command'

    def handle(self, *args, **options):
        variable = cmcid()

See Django doc for more details about commands https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/

有关命令的更多详细信息,请参阅Django doc https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/

#1


0  

You probably miss a __init__.py file (it can be just an empty file) in your views directory.

您可能会在views目录中遗漏__init__.py文件(它可能只是一个空文件)。

Add this file and it will turn your directory into a python package and the import should work

添加此文件,它将您的目录转换为python包,导入应该工作

Note that it is also required in parent directories.

请注意,父目录中也需要它。


Another possible cause is that you use the Django "machinery" in your file, as a consequence it may be not possible to run directly as a python file.

另一个可能的原因是你在文件中使用Django“机器”,因此可能无法直接作为python文件运行。

If you need to write python scripts, that interacts with your django app, you need to create a django command that will be executed with python manage.py got

如果你需要编写与你的django应用程序交互的python脚本,你需要创建一个django命令,该命令将使用python manage.py获得

Your gtt.py file needs to be in management\commands submodule of your django app.

您的gtt.py文件需要位于django应用程序的management \ commands子模块中。

It must have the following structure

它必须具有以下结构

from django.core.management.base import BaseCommand
from path.to.mpg import cmcid

class Command(BaseCommand):
    help = 'Your custom command'

    def handle(self, *args, **options):
        variable = cmcid()

See Django doc for more details about commands https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/

有关命令的更多详细信息,请参阅Django doc https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/