0
我在Ubuntu上使用django 1.11,但非虛擬python有1.6。還有其他應用程序使用1.6。當我通過Apache mod_wsgi運行我的應用程序時,它使用1.6而不是1.11,並且我得到堆棧跟蹤。Django無法在virutalenv中使用django版本
的Apache的conf:
WSGIPythonPath /home/wondi/envs/my_app
<VirtualHost *:80>
ServerName server.com
ServerAdmin [email protected]
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
WSGIDaemonProcess server.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup server.com
WSGIPassAuthorization On
WSGIScriptAlias /my_app /home/wondi/envs/my_app/my_app/wsgi.py
<Directory "/home/wondi/envs/my_app/my_app/">
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/wondi/envs/my_app/app/static/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
IndexOptions FancyIndexing
</Directory>
...
</VirtualHost>
程序my_app \ wsgi.py:
import os
import sys
from django.core.wsgi import get_wsgi_application
import site
env_path = '/home/wondi/envs/lib/python2.7/site-packages'
# we add currently directory to path and change to it
working_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(working_dir)
sys.path = [working_dir] + sys.path
# Append paths
site.addsitedir(env_path)
sys.path.append('/home/wondi/envs/')
sys.path.append('/home/wondi/envs/my_app/')
os.environ["DJANGO_SETTINGS_MODULE"] = "my_app.settings"
activate_this = "/home/wondi/envs/bin/activate_this.py"
execfile(activate_this, dict(__file__=activate_this))
application = get_wsgi_application()
堆棧跟蹤:
Django Version: 1.6.1
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.gis',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_gis',
'corsheaders',
'my_app']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
99. resolver_match = resolver.resolve(request.path_info)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
337. for pattern in self.url_patterns:
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
365. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
360. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/home/wondi/envs/my_app/my_app/urls.py" in <module>
19. from rest_framework.urlpatterns import format_suffix_patterns
File "/home/wondi/envs/lib/python2.7/site-packages/rest_framework/urlpatterns.py" in <module>
5. from rest_framework.compat import RegexURLResolver, include
File "/home/wondi/envs/lib/python2.7/site-packages/rest_framework/compat.py" in <module>
12. from django.apps import apps
Exception Type: ImportError at/
Exception Value: No module named apps
是療法e我錯過了什麼或者可以做什麼來解決這個問題?
我會說,你應該修改的sys.path之前激活的virtualenv或導入任何內容你導入django.core.wsgi的那一分鐘,你從你的virtualenv中導入了django。當你運行激活(不應該是一個shell腳本?)你修改你的env變量,覆蓋你設置的變量。所以嘗試重新排序你的wsgi.py(字面意思是反向)。 – cowbert
我完全明白。這正是你所說的。它正在工作。你能把它作爲答案嗎? – wondie
閱讀http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html,其中顯示了使用mod_wsgi設置虛擬環境的首選方式。 –