如何从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())