Django未能在virutalenv中使用django版本

时间:2017-10-05 18:42:28

标签: python django virtualenv mod-wsgi

我在Ubuntu上使用django 1.11,但非虚拟python有1.6。还有其他使用1.6的应用程序。当我通过apache mod_wsgi运行我的应用程序时,它使用1.6而不是1.11,我得到堆栈跟踪。

Apache conf:

    WSGIPythonPath /home/wondi/envs/my_app
    <VirtualHost *:80>
        ServerName server.com
        ServerAdmin admin@server.com

        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

我有什么遗漏或可以解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

您无需编辑wsgi.py即可激活virtualenv。 您需要以正确的方式运行configure apache配置。 使用WSGIDaemonProcess提及将从virtuaenv运行django项目的项目路径virtualenv路径。

WSGIDaemonProcess myproject python-home=/home/sammy/myproject/myprojectenv python-path=/home/sammy/myproject

如下所述:

也请按照本教程了解django https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04

的apache