Running a Python script outside of Django

前端 未结 7 1911
后悔当初
后悔当初 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 04:08

    a simple way:

    $ python manage.py shell -c 'import runpy; runpy.run_path("scripts/script_to_run.py")'
    

    where scripts/script_to_run.py is the desired script to run!

    you can create a bash script called "runscript.py":

    #!/bin/bash
    python manage.py shell -c 'import runpy; runpy.run_path("'$1'")'
    

    then run:

    $ runscript.py scripts/script_to_run.py
    
    0 讨论(0)
提交回复
热议问题