如何从 Django shell 执行 Python 脚本? [英] How to execute a Python script from the Django shell?

查看:41
本文介绍了如何从 Django shell 执行 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 Django shell 执行一个 Python 脚本.我试过了:

./manage.py shell <<my_script.py

但是没有用.只是等着我写点什么.

解决方案

<< 部分有误,改用 < :

$ ./manage.py shell <脚本文件

你也可以这样做:

$ ./manage.py shell...>>>execfile('myscript.py')

对于python3,你需要使用

<预><代码>>>>exec(open('myscript.py').read())

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.

解决方案

The << part is wrong, use < instead:

$ ./manage.py shell < myscript.py

You could also do:

$ ./manage.py shell
...
>>> execfile('myscript.py')

For python3 you would need to use

>>> exec(open('myscript.py').read())

这篇关于如何从 Django shell 执行 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆