在Django部署时无法导入模块

时间:2013-08-07 04:40:13

标签: python django virtualenv

我正在尝试在我的服务器上部署Django项目,我正在使用virtualenv激活部署Django。

继承了我从apache2日志的记录

[Wed Aug 07 04:25:19 2013] [error]     __import__(name)
[Wed Aug 07 04:25:19 2013] [error]   File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/project/access_and_security/urls.py", line 31, in <module>
[Wed Aug 07 04:25:19 2013] [error]     from openstack_dashboard.dashboards.project.access_and_security.\\
[Wed Aug 07 04:25:19 2013] [error]   File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/project/access_and_security/security_groups/urls.py", line 24, in <module>
[Wed Aug 07 04:25:19 2013] [error]     from openstack_dashboard.dashboards.project.access_and_security.\\
[Wed Aug 07 04:25:19 2013] [error]   File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/project/access_and_security/security_groups/views.py", line 37, in <module>
[Wed Aug 07 04:25:19 2013] [error]     from openstack_dashboard.dashboards.project.access_and_security.\\
[Wed Aug 07 04:25:19 2013] [error]   File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/project/access_and_security/security_groups/forms.py", line 35, in <module>
[Wed Aug 07 04:25:19 2013] [error]     from horizon.utils.validators import validate_ip_protocol
[Wed Aug 07 04:25:19 2013] [error] ImportError: cannot import name validate_ip_protocol

这是我得到的全部追溯

Environment:


Request Method: GET
Request URL: http://192.168.1.150:8060/

Django Version: 1.4.5
Python Version: 2.7.3
Installed Applications:
('openstack_dashboard',
 'django.contrib.contenttypes',
 'django.contrib.auth',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'compressor',
 'horizon',
 'openstack_dashboard.dashboards.project',
 'openstack_dashboard.dashboards.admin',
 'openstack_dashboard.dashboards.settings',
 'openstack_auth')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'horizon.middleware.HorizonMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)
File "/usr/lib/python2.7/dist-packages/django/middleware/locale.py" in process_request
  21.         check_path = self.is_language_prefix_patterns_used()
File "/usr/lib/python2.7/dist-packages/django/middleware/locale.py" in is_language_prefix_patterns_used
  54.         for url_pattern in get_resolver(None).url_patterns:
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/var/www/projects/openstack-horizon/openstack_dashboard/urls.py" in <module>
  38.     url(r'', include(horizon.urls))
File "/usr/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
  25.     patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/utils/functional.py" in inner
  184.             self._setup()
File "/usr/lib/python2.7/dist-packages/django/utils/functional.py" in _setup
  248.         self._wrapped = self._setupfunc()
File "/usr/lib/python2.7/dist-packages/horizon/base.py" in url_patterns
  715.             return self._urls()[0]
File "/usr/lib/python2.7/dist-packages/horizon/base.py" in _urls
  746.                     url(r'^%s/' % dash.slug, include(dash._decorated_urls)))
File "/usr/lib/python2.7/dist-packages/horizon/base.py" in _decorated_urls
  456.                     url(r'^%s/' % panel.slug, include(panel._decorated_urls)))
File "/usr/lib/python2.7/dist-packages/horizon/base.py" in _decorated_urls
  230.         urlpatterns = self._get_default_urlpatterns()
File "/usr/lib/python2.7/dist-packages/horizon/base.py" in _get_default_urlpatterns
  83.                 urls_mod = import_module('.urls', package_string)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/admin/instances/urls.py" in <module>
  24. from openstack_dashboard.dashboards.admin.instances.views import AdminIndexView
File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/admin/instances/views.py" in <module>
  31. from openstack_dashboard.dashboards.admin.instances.tables import \
File "/var/www/projects/openstack-horizon/openstack_dashboard/dashboards/admin/instances/tables.py" in <module>
  25. from horizon.utils.filters import parse_isotime

Exception Type: ImportError at /
Exception Value: cannot import name parse_isotime

继承我的虚拟主机

<VirtualHost *:8060>
        ServerAdmin jonathan.c@advitor.com
        ServerName automation-cloud.com
        ServerAlias www.automation-cloud.com
        WSGIScriptAlias / var/www/projects/openstack-horizon/horizon.wsgi

        Alias /static/ /var/www/projects/openstack-horizon/openstack_dashboard/static/
        <Location "/static/">
            Options -Indexes
        </Location>
</VirtualHost>

继承人我的horizo​​n.wsgi文件

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/www/projects/openstack-horizon/.venv/local/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/projects/openstack-horizon')
#sys.path.append('/var/www/projects/openstack-horizon/horizon')
sys.path.append('/var/www/projects/openstack-horizon/openstack_dashboard')
#/usr/lib/python2.7/dist-packages/horizon/
sys.path.append('/usr/lib/python2.7/dist-packages/horizon')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

# Activate your virtual env
activate_env=os.path.expanduser("/var/www/projects/openstack-horizon/.venv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我检查了所有内容,Horizo​​n app位于settings.py

这个Django项目在运行python manage.py runserver virtualenv时运行正常。

1 个答案:

答案 0 :(得分:1)

Apache没有对您的虚拟环境进行寻址。检查错误跟踪的第一行

/usr/lib/python2.7/dist-packages/django/core/handlers/base.py

它指向默认的python位置而不是你的虚拟环境。 尝试这包括虚拟环路径。

sys.path.append('/var/www/projects/openstack-horizon/.venv/local/lib/python2.7/site-packages')

此致

Ansh Jain