如何执行包含django函数的python脚本

时间:2023-01-10 00:03:50

I have a python script test.py that imports make_password function from Django library. It does it like this:

我有一个python脚本test.py从Django库导入make_password函数。它是这样的:

from django.contrib.auth.hashers import make_password

.... a lot of other stuff
def init():
    ...

if __name__ == "__main__":
    init()

So, I want to run it from the command line like:

所以,我想从命令行运行它,如:

python test.py

But this results in a whole list of error messages, which are all about importing Django module. If I comment out from django.contrib.auth.hashers import make_password, then everything is ok (except the fact that I can't use make_password function).

但这导致了一整套错误消息,这些消息都是关于导入Django模块的。如果我从django.contrib.auth.hashers导出make_password注释掉,那么一切正常(除了我不能使用make_password函数)。

1 个解决方案

#1


1  

The error messages your are getting are most likely complaints that the settings module is not available.

您获得的错误消息很可能是设置模块不可用的投诉。

Django initializes its runtime environment (via django.core.management.setup_environ()) when you import any of its core modules. The django ecosystem is not meant to function without a valid project environment.

当您导入任何核心模块时,Django会初始化其运行时环境(通过django.core.management.setup_environ())。如果没有有效的项目环境,django生态系统就无法运行。

You have at least two options:

您至少有两个选择:

1. Manually set django settings module

1.手动设置django设置模块

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourapp.settings'

from django.contrib.auth.hashers import make_password
...

Note that this requires yourapp.settings to be available via sys.path, so it might be necessary to explicitly add the project path to sys.path (or PYTHON_PATH).

请注意,这需要通过sys.path提供youpp。设置,因此可能需要将项目路径显式添加到sys.path(或PYTHON_PATH)。

Other versions of basically the same thing are available.

其他版本的基本相同的东西都可用。

2. Build a management command

2.建立管理命令

This is explained in some detail in the documentation.

这在文档中有详细解释。

In principle, you need to provide a yourapp.management.mycommand module that contains a single Command class:

原则上,您需要提供包含单个Command类的yourapp.management.mycommand模块:

class Command(BaseCommand):
    def handle(self, *args, **options):
       # make_password()

The command can be run through django's manage.py.

该命令可以通过django的manage.py运行。

#1


1  

The error messages your are getting are most likely complaints that the settings module is not available.

您获得的错误消息很可能是设置模块不可用的投诉。

Django initializes its runtime environment (via django.core.management.setup_environ()) when you import any of its core modules. The django ecosystem is not meant to function without a valid project environment.

当您导入任何核心模块时,Django会初始化其运行时环境(通过django.core.management.setup_environ())。如果没有有效的项目环境,django生态系统就无法运行。

You have at least two options:

您至少有两个选择:

1. Manually set django settings module

1.手动设置django设置模块

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourapp.settings'

from django.contrib.auth.hashers import make_password
...

Note that this requires yourapp.settings to be available via sys.path, so it might be necessary to explicitly add the project path to sys.path (or PYTHON_PATH).

请注意,这需要通过sys.path提供youpp。设置,因此可能需要将项目路径显式添加到sys.path(或PYTHON_PATH)。

Other versions of basically the same thing are available.

其他版本的基本相同的东西都可用。

2. Build a management command

2.建立管理命令

This is explained in some detail in the documentation.

这在文档中有详细解释。

In principle, you need to provide a yourapp.management.mycommand module that contains a single Command class:

原则上,您需要提供包含单个Command类的yourapp.management.mycommand模块:

class Command(BaseCommand):
    def handle(self, *args, **options):
       # make_password()

The command can be run through django's manage.py.

该命令可以通过django的manage.py运行。