2017-03-23 1 views
1

J'ai un problème avec Apache2.4 + mod_wsgi + Python3.6 (sur win10 x64).
Quand je suis en train de site d'accès, je reçois cette erreur dans Apache:Django + Apache ModuleNotFoundError: Aucun module nommé 'myproject'

[Wed Mar 22 22:52:19.162397 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] mod_wsgi (pid=10872): Target WSGI script 'C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py' cannot be loaded as Python module. 
[Wed Mar 22 22:52:19.162397 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] mod_wsgi (pid=10872): Exception occurred processing WSGI script 'C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py'. 
[Wed Mar 22 22:52:19.163396 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] Traceback (most recent call last):\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py", line 16, in <module>\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  application = get_wsgi_application()\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\site-packages\\django\\core\\wsgi.py", line 13, in get_wsgi_application\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  django.setup(set_prefix=False)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\site-packages\\django\\__init__.py", line 22, in setup\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 53, in __getattr__\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  self._setup(name)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 41, in _setup\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  self._wrapped = Settings(settings_module)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 97, in __init__\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  mod = importlib.import_module(self.SETTINGS_MODULE)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "c:\\program files\\python36\\lib\\importlib\\__init__.py", line 126, in import_module\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227]  return _bootstrap._gcd_import(name[level:], package, level)\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 978, in _gcd_import\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 961, in _find_and_load\r 
[Wed Mar 22 22:52:19.163893 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked\r 
[Wed Mar 22 22:52:19.164394 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed\r 
[Wed Mar 22 22:52:19.164394 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 978, in _gcd_import\r 
[Wed Mar 22 22:52:19.164394 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 961, in _find_and_load\r 
[Wed Mar 22 22:52:19.164394 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked\r 
[Wed Mar 22 22:52:19.164394 2017] [wsgi:error] [pid 10872:tid 1256] [client 176.8.x.x:32227] ModuleNotFoundError: No module named 'myproject'\r 
[Wed Mar 22 22:52:19.768710 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] mod_wsgi (pid=10872): Target WSGI script 'C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py' cannot be loaded as Python module., referer: http://mysite/ 
[Wed Mar 22 22:52:19.768710 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] mod_wsgi (pid=10872): Exception occurred processing WSGI script 'C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py'., referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] Traceback (most recent call last):\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py", line 16, in <module>\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  application = get_wsgi_application()\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\site-packages\\django\\core\\wsgi.py", line 13, in get_wsgi_application\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  django.setup(set_prefix=False)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\site-packages\\django\\__init__.py", line 22, in setup\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 53, in __getattr__\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  self._setup(name)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 41, in _setup\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  self._wrapped = Settings(settings_module)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\site-packages\\django\\conf\\__init__.py", line 97, in __init__\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  mod = importlib.import_module(self.SETTINGS_MODULE)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "c:\\program files\\python36\\lib\\importlib\\__init__.py", line 126, in import_module\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229]  return _bootstrap._gcd_import(name[level:], package, level)\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 978, in _gcd_import\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 961, in _find_and_load\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.769712 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.770211 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.770211 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 978, in _gcd_import\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.770211 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 961, in _find_and_load\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.770211 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked\r, referer: http://mysite/ 
[Wed Mar 22 22:52:19.770211 2017] [wsgi:error] [pid 10872:tid 1360] [client 176.8.x.x:32229] ModuleNotFoundError: No module named 'myproject'\r, referer: http://mysite/ 

Voici mes fichiers de configuration:

wsgi.py:

import os 
from django.core.wsgi import get_wsgi_application 
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings" 
application = get_wsgi_application() 

paramètres .py:

import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

SECRET_KEY = 'secret_key_here' 

DEBUG = False 

ALLOWED_HOSTS = ['mysite'] 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'site.apps.SiteConfig', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'myproject.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'myproject.wsgi.application' 


DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'name', 
     'USER': 'root', 
     'PASSWORD': 'pass', 
     'HOST': 'host', 
     'PORT': '3306', 
    } 
} 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

STATIC_URL = '/static/' 
STATIC_ROOT = 'C:/wampstack-5.6.30-1/apache2/htdocs/static.mysite/' 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

httpd-vhosts.conf (Apache):

<VirtualHost *:80> 
WSGIScriptAlias/"C:/wampstack-5.6.30-1/apache2/htdocs/myproject/myproject/wsgi.py" 

ServerName mysite 
Alias /static "C:/wampstack-5.6.30-1/apache2/htdocs/myproject/static" 

<Directory "C:/wampstack-5.6.30-1/apache2/htdocs/myproject"> 
Order allow,deny 
Allow from all 
</Directory> 
</VirtualHost> 

En httpd.conf J'ai ajouté ce code pour le démarrage mod_wsgi normal:

LoadFile "c:/program files/python36/python36.dll" 
LoadModule wsgi_module "c:/program files/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd" 
WSGIPythonPath "c:/program files/python36;C:/program files/Python36/Lib;C:/program files/Python36/Lib/site-packages;C:/program files/Python36/DLLs" 
WSGIPythonHome "c:/program files/python36" 

post-scriptum ' myproject' - nom du projet; ' mysite' - nom du site.

+0

Il est mieux pour ajouter l'erreur stacktrace/summary dans la question.Le lien pourrait être supprimé/supprimé à l'avenir. –

+0

Jetez un oeil à http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/ et éditez le fichier wsgi en conséquence. – itzMEonTV

+0

Pourquoi avez-vous réglé '' WSGIPythonPath'' à ce que vous avez fait? Cela ne devrait pas être nécessaire. Définissez '' WSGIPythonPath'' dans le répertoire parent de '' myproject'', ou ajoutez ce chemin à '' sys.path'' dans le fichier de script WSGI. –

Répondre

0

Ajouté à mon wsgi.py:

sys.path.append('/home/django_projects/MyProject') 
sys.path.append('/home/django_projects/MyProject/myproject') 

Tout fonctionne bien maintenant :)

1

Comme j'utilisais WSGIDaemonProcess (comme recommened en divers endroits) J'ai choisi d'ajouter chemin de python sur ce ligne de mon fichier apache app.conf. Notez que le Django docs mentionne que WSGIPythonPath ne fonctionne pas lorsque vous utilisez WSGIDaemonProcess.

La ligne dans mon app.conf est donc:

WSGIDaemonProcess app python-home=/usr/share/virtualenvs/myapp user=myappuser threads=15 python-path=/my/app/installed/dir/ 

Je préfère cela à la définition du chemin dans wsgi.py parce que cela signifiait ne pas avoir à écrire plus méta-config pour traiter les différents chemins entre mon développement (ordinateur portable) et les serveurs de production. Je suis assez nouveau à ce sujet donc a) accueillir tous les commentaires b) ne pas suivre cette réponse à l'aveuglette!

0

J'ai eu cette erreur dans mon projet de django que j'avais déployé dans l'hôte virtuel apache:

ImportError: Aucun module nommé « monsite »

Il est à cause de mauvaise configuration dans mon vhost dans apache . I a mis en django 1.9 config et je l'ai corrigé à Django 2.0 config correcte et simple:

WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com 
WSGIProcessGroup example.com 

refference

Django 2: https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/#daemon-mode Django 1.9:: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#daemon-mode