[英]Are django modules global - ImportError: No module named django.core.management?
I am quite confused with how django works. 我对Django的工作方式感到困惑。 I followed some guides and tested a sample app and got it running on my local dev machine. 我遵循了一些指南,并测试了一个示例应用程序,并使其在我的本地开发机上运行。 It works great. 效果很好。 But when I moved this sample app to my production server, I got this error: 但是,当我将此示例应用程序移至生产服务器时,出现以下错误:
# cd /var/www/html/helloapp
# python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
So, during my setup for the sample app, I did this: 因此,在设置示例应用程序的过程中,我这样做了:
$ sudo apt-get install python-django
Then: 然后:
$ django-admin startproject helloapp
Then: 然后:
$ cd /var/www/html/django-project/helloapp/
Then: 然后:
$ python manage.py runserver
Result: 结果:
It worked! 有效! Congratulations on your first Django-powered page. 恭喜,您的第一个Django驱动页面。
So, I transferred the files helloapp/ into my production server: 因此,我将文件helloapp /转移到了生产服务器中:
# cd /var/www/html/helloapp
# python manage.py runserver
Then there is the error: 然后是错误:
No module named django.core.management 没有名为django.core.management的模块
Do I have to install python-django
in my production server - "globally" as one of my system software in my production server? 我是否必须在生产服务器中“全球安装”安装python-django
作为生产服务器中的系统软件之一?
What is django.core.management
for actually? django.core.management
实际上是什么? There might be many this kind of modules I need to run my app. 运行我的应用程序可能需要很多这类模块。 So are these modules coming from django's "global" then? 那么,这些模块是否来自Django的“全局”模块?
If my system broke or I removed python-django
one day, all my django apps will break too? 如果我的系统坏了或者有一天我删除了python-django
,我所有的django应用程序也会坏掉吗?
EDIT: 编辑:
I got it running with a virtual env instead, but it seems to have problems with gunicorn
: 我改为使用虚拟环境运行它,但是gunicorn
似乎有问题:
$ gunicorn helloapp.wsgi
[2017-05-18 22:33:37 +0000] [882] [INFO] Starting gunicorn 19.6.0
[2017-05-18 22:33:37 +0000] [882] [INFO] Listening at: http://127.0.0.1:8000 (882)
[2017-05-18 22:33:37 +0000] [882] [INFO] Using worker: sync
[2017-05-18 22:33:37 +0000] [886] [INFO] Booting worker with pid: 886
[2017-05-18 22:33:37 +0000] [886] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 557, in spawn_worker
worker.init_process()
File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 136, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 384, in import_app
__import__(module)
File "/var/www/html/django-project/helloapp/helloapp/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
[2017-05-18 22:33:37 +0000] [886] [INFO] Worker exiting (pid: 886)
[2017-05-18 22:33:37 +0000] [882] [INFO] Shutting down: Master
[2017-05-18 22:33:37 +0000] [882] [INFO] Reason: Worker failed to boot.
Can you confirm you have Django
installed? 您可以确认已安装Django
吗?
sudo pip install django --upgrade
To use virtualenv, first install it 要使用virtualenv,请先安装
pip install virtualenv
Create a new env (should not be inside the Django project) 创建一个新的环境(不应在Django项目中)
virtualenv my_env
Inside this folder, you can activate the env 在此文件夹中,您可以激活环境
. bin/activate
Once the env is active, you install django 启用环境后,安装django
pip install django
Then go back to the django project and running 然后回到django项目并运行
python manage.py runserver
should work 应该管用
Yes, you must have django installed on your production server. 是的,您必须在生产服务器上安装django。 I personally use virtual environments for this matter, try 我个人为此使用虚拟环境,请尝试
sudo apt-get install python-virtualenv
virtualenv env_name
source env_name/bin/activate
Once you have done that you should see the name of your environment written in front of your pwd. 完成后,您应该看到环境名称写在pwd的前面。 This will allow you to install python packages ONLY on that environment. 这将允许您仅在该环境上安装python软件包。 But, of course, you're gonna have to use this in your production server as well... 但是,当然,您也将不得不在生产服务器中使用它...
Do I have to install python-django in my production server 我是否必须在生产服务器中安装python-django
Yes, django must be installed on your production server the same way you installed on your development server 是的,django必须以与开发服务器相同的方式安装在生产服务器上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.