问题描述
我试图在我的python脚本中使用我的django应用程序模型。
这里是我的方法
import os,sys
sys.path.append( / var / www / cloudloon / horizon)
os.environ [DJANGO_SETTINGS_MODULE] =openstack_dashboard.settings
from django.contrib.auth.models import User
我很困惑为什么它给我
ImportError:无法导入设置openstack_dashboard.settings(是否在sys.path?):无法导入名称auth
检查后,我创建了一个名为creds的文件,其中包含
export PYTHONPATH = $ PYTHONPATH:/ var / www / cloudloon /地平线/;
export DJANGO_SETTINGS_MODULE = openstack_dashboard.settings; django-admin.py shell;
和从文件所在的终端窗口,我做
源文件
从那个django -admin.py shell,我可以导入任何我的django应用程序模型没有任何错误。
为什么它不能在我的python脚本?
我完成了Django,我需要做的是创建一个可以访问我的django应用程序模型的python-daemon脚本。
我正在Ubuntu 12.04中使用django 1.5
在寻找解决方案时,我做到了这一点:
import os,sys
sys.path.append(/ var / www / cloudloon / horizon)
sys.path.append (/ var / www / cloudloon / horizon / openstack_dashboard)
#os.environ [DJANGO_SETTINGS_MODULE] =settings
os.environ.setdefault(DJANGO_SETTINGS_MODULE,
openstack_dashboard.settings)
print os.environ [DJANGO_SETTINGS_MODULE]
for s在sys.path中:
打印s
from django.contrib.auth.models import用户
保留输出:,而不是做这些奇怪的杂技。
创建名为管理的模块(换句话说,创建一个目录 management
并在其中创建您在 INSTALLED_APPS
中列出的任何应用的目录中都有一个空的 __ init __。py
文件)。所以如果你有 myapp
,你将创建:
myapp
|
>管理
| | __init__.py
> models.py
> views.py
然后在管理目录中创建另一个模块 commands
并在其中创建一个文件,它是您的命令的名称;例如
my_command.py
,如下所示:
myapp
|
>管理
| | __init__.py
| |命令
| | | __init__.py
| | | my_command.py
> models.py
> view.py
在 my_command.py
中,写这个样板代码:
from django.core.management.base import BaseCommand,CommandError
from myapp.models import MyModel
class Command(BaseCommand):
help ='有一些神奇的工作'
def handle(self,* args,** options):
你的工作在这里
self.stdout.write('有{}的东西!'格式(MyModel.objects.count()))
保存文件后,您可以执行 python manage.py my_command
它将可以访问所有的模型和设置。
如果您需要作为守护进程运行它,写了这是正确的。一旦你安装了它:
从django.core.management.base导入CommandError
从daemon_command import DaemonCommand
from myapp.models import MyModel
class Command(DaemonCommand):
help ='有一些神奇的工作'
def loop_callback(self,* args, **选项)
你的工作在这里
self.stdout.write('有{}的东西!'格式(MyModel.objects.count()))
一旦你完成了:
这个过程的重要部分是这些:
*它在服务器启动时自动出现
*它将错误和信息记录到命名位置,如果进程死机,则可以配置
*,它直接重新启动
[...]
正常运行命令,但通过--start之一,--stop或--restart到
作为守护进程工作。否则,该命令将作为标准应用程序运行。
I was very confused today.
I was trying to use my django app models in my python script.
here is my approach
import os, sys
sys.path.append("/var/www/cloudloon/horizon")
os.environ["DJANGO_SETTINGS_MODULE"] = "openstack_dashboard.settings"
from django.contrib.auth.models import User
I was confused why its giving me
ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path?): cannot import name auth
Upon checking I created a a file called creds that includes
export PYTHONPATH=$PYTHONPATH:/var/www/cloudloon/horizon/;
export DJANGO_SETTINGS_MODULE=openstack_dashboard.settings; django-admin.py shell;
and from terminal window where creds file is located, I do
source creds
and from that django-admin.py shell, I could import any of my django app models without any errors.
Why it doesn't work in my python script?
I am done with Django, What I need to do is to create a python-daemon script that will access my django app models.
I am working with in Ubuntu 12.04 that has django 1.5
As I looking for solutions, I did this:
import os, sys
sys.path.append("/var/www/cloudloon/horizon")
sys.path.append("/var/www/cloudloon/horizon/openstack_dashboard")
# os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"openstack_dashboard.settings")
print os.environ["DJANGO_SETTINGS_MODULE"]
for s in sys.path:
print s
from django.contrib.auth.models import User
heres the output: http://paste.openstack.org/show/48787/
as you can see, the directory where settings.py is located are present on my sys.path, however, It was still unable to import openstack_dashboard.settings.
Thanks everyone.
You need to write a custom management command, instead of doing these weird acrobatics.
Create a module called management (in other words, create a directory management
and inside it create an empty __init__.py
file) inside the directory of any app that you have listed in INSTALLED_APPS
. So if you have myapp
, you would create:
myapp
|
> management
| | __init__.py
> models.py
> views.py
Then in the management directory, create another module commands
and in it create a file which is the name of your command; for example my_command.py
, like this:
myapp
|
> management
| | __init__.py
| | commands
| | | __init__.py
| | | my_command.py
> models.py
> views.py
In my_command.py
, write this boilerplate code:
from django.core.management.base import BaseCommand, CommandError
from myapp.models import MyModel
class Command(BaseCommand):
help = 'Does some magical work'
def handle(self, *args, **options):
""" Do your work here """
self.stdout.write('There are {} things!'.format(MyModel.objects.count()))
Once you save the file, you'll be able to do python manage.py my_command
and it will have access to all your models and settings.
If you need to run it as a daemon, Daniel Roseman wrote django-initd
which does exactly that. Once you have installed it:
from django.core.management.base import CommandError
from daemon_command import DaemonCommand
from myapp.models import MyModel
class Command(DaemonCommand):
help = 'Does some magical work'
def loop_callback(self, *args, **options):
""" Do your work here """
self.stdout.write('There are {} things!'.format(MyModel.objects.count()))
Once you have that done from the github readme:
The important parts of such a process are these:
* it comes up automatically on server startup
* it logs errors and information to a named location, which is configurable
* if the process dies, it restarts itself straight away
[...]
Run the command as normal, but pass one of --start, --stop or --restart to
work as a daemon. Otherwise, the command will run as a standard application.
这篇关于在外部python脚本中使用Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!