Running a Python script outside of Django

前端 未结 7 1919
后悔当初
后悔当初 2021-01-30 03:22

I have a script which uses the Django ORM features, amongst other external libraries, that I want to run outside of Django (that is, executed from the command-line).

Edi

7条回答
  •  心在旅途
    2021-01-30 03:55

    All you need is importable settings and properly set python path. In the most raw form this can be done by setting up appropriate environment variables, like:

    $ DJANGO_SETTINGS_MODULE=myproject.settings PYTHONPATH=$HOME/djangoprojects python myscript.py
    

    There are other ways, like calling settings.configure() and already mentioned setup_environ() described by James Bennett in some blog post.

提交回复
热议问题