2017-03-02 65 views
3

嘗試使用apache2和mod_wsgi託管我的django項目。我已經試過各種配置,但要在瀏覽器訪問該網站,例如當越來越導入錯誤:Apache/Django:ImportError:沒有名爲'my_project'的模塊

Traceback (most recent call last): 
    File "/home/user/projects/my_projectMain/my_project/my_project/wsgi.py", line 19, in <module> 
    application = get_wsgi_application() 
    File "/usr/local/lib/python3.4/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application 
    django.setup(set_prefix=False) 
    File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 22, in setup 
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) 
    File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 53, in __getattr__ 
    self._setup(name) 
    File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 41, in _setup 
    self._wrapped = Settings(settings_module) 
    File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 97, in __init__ 
    mod = importlib.import_module(self.SETTINGS_MODULE) 
    File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 2254, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 2237, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed 
    File "<frozen importlib._bootstrap>", line 2254, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 2237, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked 
ImportError: No module named 'my_project' 

這裏是我的目錄結構(/家/用戶/項目/ my_projectMain /):

-- my_projectEnv\ (virtualenv w/ python 3.5.2, django 1.10, django-bootstrap3, django-static-jquery==2.1.4) 
-- my_project\ 
|-- app\ 
|-- manage.py 
|-- static\ 
|-- my_project\ 
|-- | -- __init__.py 
|-- | -- __pycache__ 
|-- | -- settings.py 
|-- | -- wsgi.py 
`-- | -- urls.py 

wsgi.py:

import os 

from django.core.wsgi import get_wsgi_application 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings") 

application = get_wsgi_application() 

/etc/apache2/sites-available/000-default.conf:

... 
#ServerName www.example.com 

ServerAdmin [email protected] 
DocumentRoot /var/www/html  

Alias /static /home/user/projects/my_projectMain/my_project/static 
     <Directory /home/user/projects/my_projectMain/my_project/static> 
      Require all granted 
     </Directory> 

     <Directory /home/user/projects/my_projectMain/my_project/my_project> 
      <Files wsgi.py> 
       Require all granted 
      </Files> 
     </Directory> 

     WSGIDaemonProcess my_project python-path=/home/user/projects python-home=/home/user/projects/my_projectMain/my_projectEnv 
     WSGIProcessGroup my_project 
     WSGIScriptAlias//home/user/projects/my_projectMain/my_project/my_project/wsgi.py 
... 

我很擔心我的目錄結構是問題,但也看到很多與wsgi.py中路徑相關的其他帖子不正確。


編輯: 如果我改變的python-路徑:

WSGIDaemonProcess my_project python-path=/home/user/projects/my_projectMain/my_project/my_project 

我得到:

Traceback (most recent call last): 
    File "/home/user/projects/my_projectMain/my_project/my_project/wsgi.py", line 25, in <module> 
    application = get_wsgi_application() 
    File "/usr/local/lib/python3.4/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application 
    django.setup(set_prefix=False) 
    File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 27, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 85, in populate 
    app_config = AppConfig.create(entry) 
    File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 90, in create 
    module = import_module(entry) 
    File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 2254, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 2237, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked 
ImportError: No module named 'django_static_jquery' 
  1. 這是否意味着我已經得到了過去,它不能夠導入my_project?
  2. 我已經運行了collectstatic並確保static_jquery在我的STATIC_ROOT中,它怎麼能不能導入已安裝的應用程序?
+0

可能不相關,但不能將Python 3.4編譯的mod_wsgi與使用Python 3.5構建的Python虛擬環境配合使用。混合這些流是不好的,會導致各種問題。請參閱http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html,它會對此提出警告。 –

+0

我也注意到了,只要您認爲它與上述問題無關,我會稍後再解決。 – brewcrazy

+0

我會先修復它。如果沒有正確匹配,沒有保證。還要檢查你的目錄/文件是否具有Apache運行的用戶可訪問的權限。 –

回答

0

在Apache的配置,WSGIDaemonProcess指令:

python-path是不正確或不完整的。對於my_project是可導入的,您必須將/home/pi/projects/my_projectMain/my_project添加到python路徑。

例如:在該文件中

WSGIDaemonProcess my_project python-path=/home/user/projects/my_projectMain/my_project python-home=/home/user/projects/my_projectMain/my_projectEnv 

仔細檢查所有的配置,它可能是pi,而不是user最多的地方。包括上面的行。

+0

用戶在我的文件中適合任何地方。我只是嘗試泛化上面的所有內容,並錯過了堆棧跟蹤中的一行。 – brewcrazy

+0

'WSGIDaemonProcess my_project python-path =/home/user/projects/my_projectMain/my_project'會產生相同的錯誤:ImportError:沒有名爲'my_project'的模塊 – brewcrazy

+0

只檢查你是否改變了它:它應該是'pi'以及 – spectras