使用django:从“python管理”。py shell "到python脚本

时间:2021-12-31 18:04:36

I can move to a python project directory (say c:\www\myproject) and then issue

我可以移动到一个python项目目录(比如c:\www\myproject),然后发出

   python manage.py shell

and then I can use all modules from django project, say the following piece of commands from the shell command:

然后,我可以使用来自django项目的所有模块,比如下面的shell命令中的命令:

import settings 
from django.template import Template, Context

t=Template("My name is {myname}.")
c=Context({"myname":"John"})
f = open('write_test.txt', 'w')
f.write(t.render(c))
f.close

now, when I tried to collect all my commands into a python script, say "mytest.py", I cannot execute the script. I must missed something important.

现在,当我试图将所有命令收集到python脚本中时,请输入“mytest”。我不能执行脚本。我一定是漏掉了什么重要的东西。

I issued python mytest.py

我发布了python mytest.py

then I got Import error: could not import settings Is it on sys path?"

然后我得到导入错误:不能导入设置,是在sys路径上吗?

I'm in the project directory where settings.py resides.....

我在设置的项目目录中。py驻留.....

Could some one help me out?

有人能帮我一下吗?

thanks.

谢谢。

8 个解决方案

#1


21  

Try using a Django management command instead.

尝试使用Django管理命令。

# myproject/myapp/management/commands/my_command.py

from django.core.management.base import NoArgsCommand
from django.template import Template, Context
from django.conf import settings

class Command(NoArgsCommand):
    def handle_noargs(self, **options):
        t=Template("My name is {myname}.")
        c=Context({"myname":"John"})
        f = open('write_test.txt', 'w')
        f.write(t.render(c))
        f.close

And then (if you follow the docs) you will be able to execute the command in the following fashion:

然后(如果您遵循文档),您将能够以以下方式执行命令:

python manage.py my_command

#2


23  

This method is deprecated in Django 1.4. Use django.conf.settings.configure() instead (see @adiew's answer for example code).

在Django 1.4中不支持这个方法。使用django.conf.settings.configure()代替(参见@adiew的答案示例代码)。

Old method follows.

老方法。

Put this at the beginning of your script

把这个放在脚本的开头

from django.core.management import setup_environ
import settings
setup_environ(settings)

This is really what the manage.py does behind the scene. To see it view the Django source in django/core/management/__init__.py. After executing these lines everything should be just like in ./manage.py shell.

这才是真正的管理者。py在幕后工作。在Django /core/management/__init__.py中查看Django源代码。在执行完这些行之后,一切都应该像in ./管理。py壳。

#3


11  

Try put these two lines at the beginning of your script:

试着把这两行写在剧本的开头:

from django.conf import settings
settings.configure() # check django source for more detail

# now you can import other django modules
from django.template import Template, Context

#4


3  

UPDATE: From another post.

更新:从另一个职位。

./manage.py shell < myscript.py

/管理。py壳< myscript.py

#5


2  

To import Django's settings use:

要导入Django的设置,请使用:

from django.conf import settings

#6


2  

Instead of manually adding things to your python script, or having to fit in the management command format, in case this is not something that needs to stay around long, you can get all the benefits of the Django environment by running your script with ./manage.py runscript <myscript.py>

不要手工向python脚本添加内容,或者必须使用管理命令格式,如果这不是需要长期使用的内容,那么可以通过使用./manage运行脚本获得Django环境的所有好处。py runscript < myscript.py >

... but if your script is in your project folder, then you can just add this line to the top of the python script: import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

…但是如果您的脚本在项目文件夹中,那么您可以将这一行添加到python脚本的顶部:import os;操作系统。环境(“DJANGO_SETTINGS_MODULE”)= '设置'

#7


1  

Saw in https://*.com/a/24456404/4200284 a good solution for Django >= 1.7 and in case someone uses django-configurations this worked for me:

在https://*.com/a/24456404/4200284中可以看到Django >= 1.7的一个很好的解决方案。

import sys, os, django

sys.path.append('/path/to/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.local") # path to config

## if using django-configurations
os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
from configurations import importer
importer.install()

django.setup() ## apparently important for django 1.7

from foo.models import Bar

print Bar.objects.all()

#8


0  

Here is yet another variant: I wrote and often use a management command "run" which has the advantage that scripts can see their command-line parameters:

这里还有另一个变体:我编写并经常使用管理命令“run”,它的优点是脚本可以看到它们的命令行参数:

http://lino-framework.org/api/lino.management.commands.run.html

http://lino-framework.org/api/lino.management.commands.run.html

#1


21  

Try using a Django management command instead.

尝试使用Django管理命令。

# myproject/myapp/management/commands/my_command.py

from django.core.management.base import NoArgsCommand
from django.template import Template, Context
from django.conf import settings

class Command(NoArgsCommand):
    def handle_noargs(self, **options):
        t=Template("My name is {myname}.")
        c=Context({"myname":"John"})
        f = open('write_test.txt', 'w')
        f.write(t.render(c))
        f.close

And then (if you follow the docs) you will be able to execute the command in the following fashion:

然后(如果您遵循文档),您将能够以以下方式执行命令:

python manage.py my_command

#2


23  

This method is deprecated in Django 1.4. Use django.conf.settings.configure() instead (see @adiew's answer for example code).

在Django 1.4中不支持这个方法。使用django.conf.settings.configure()代替(参见@adiew的答案示例代码)。

Old method follows.

老方法。

Put this at the beginning of your script

把这个放在脚本的开头

from django.core.management import setup_environ
import settings
setup_environ(settings)

This is really what the manage.py does behind the scene. To see it view the Django source in django/core/management/__init__.py. After executing these lines everything should be just like in ./manage.py shell.

这才是真正的管理者。py在幕后工作。在Django /core/management/__init__.py中查看Django源代码。在执行完这些行之后,一切都应该像in ./管理。py壳。

#3


11  

Try put these two lines at the beginning of your script:

试着把这两行写在剧本的开头:

from django.conf import settings
settings.configure() # check django source for more detail

# now you can import other django modules
from django.template import Template, Context

#4


3  

UPDATE: From another post.

更新:从另一个职位。

./manage.py shell < myscript.py

/管理。py壳< myscript.py

#5


2  

To import Django's settings use:

要导入Django的设置,请使用:

from django.conf import settings

#6


2  

Instead of manually adding things to your python script, or having to fit in the management command format, in case this is not something that needs to stay around long, you can get all the benefits of the Django environment by running your script with ./manage.py runscript <myscript.py>

不要手工向python脚本添加内容,或者必须使用管理命令格式,如果这不是需要长期使用的内容,那么可以通过使用./manage运行脚本获得Django环境的所有好处。py runscript < myscript.py >

... but if your script is in your project folder, then you can just add this line to the top of the python script: import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

…但是如果您的脚本在项目文件夹中,那么您可以将这一行添加到python脚本的顶部:import os;操作系统。环境(“DJANGO_SETTINGS_MODULE”)= '设置'

#7


1  

Saw in https://*.com/a/24456404/4200284 a good solution for Django >= 1.7 and in case someone uses django-configurations this worked for me:

在https://*.com/a/24456404/4200284中可以看到Django >= 1.7的一个很好的解决方案。

import sys, os, django

sys.path.append('/path/to/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.local") # path to config

## if using django-configurations
os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
from configurations import importer
importer.install()

django.setup() ## apparently important for django 1.7

from foo.models import Bar

print Bar.objects.all()

#8


0  

Here is yet another variant: I wrote and often use a management command "run" which has the advantage that scripts can see their command-line parameters:

这里还有另一个变体:我编写并经常使用管理命令“run”,它的优点是脚本可以看到它们的命令行参数:

http://lino-framework.org/api/lino.management.commands.run.html

http://lino-framework.org/api/lino.management.commands.run.html