如何从Django Shell执行Python脚本? [英] How to execute a Python script from the Django shell?
本文介绍了如何从Django Shell执行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从Django shell执行Python脚本.我试过了:
I need to execute a Python script from the Django shell. I tried:
./manage.py shell << my_script.py
但是没有用.只是在等我写东西.
But it didn't work. It was just waiting for me to write something.
推荐答案
<<
部分错误,请改用<
:
$ ./manage.py shell < myscript.py
您也可以这样做:
$ ./manage.py shell
...
>>> execfile('myscript.py')
对于python3,您需要使用
For python3 you would need to use
>>> exec(open('myscript.py').read())
这篇关于如何从Django Shell执行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文