Apache / Django:ImportError:没有名为' my_project'

时间:2017-03-02 21:31:28

标签: python django apache mod-wsgi wsgi

尝试使用apache2和mod_wsgi来托管我的django项目。我尝试了各种配置,但在浏览器中点击网站时不断出现导入错误,例如:

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'

这是我的目录结构(/ home / user / projects / 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 webmaster@localhost
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-path改为:

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中,怎么可能无法导入已安装的应用程序?

2 个答案:

答案 0 :(得分:1)

我做了一些错误,导致了我得到的错误。其中一些对于经验丰富的用户来说可能是显而易见的,但希望写下这些课程将有助于其他人。我知道我必须深入挖掘并在许多人的位置挖出一些这样的东西。

我了解到,一旦设置了virtualenv(virtualenv -p /usr/local/bin/python3 <path/to/new/virtualenv/> btw),您应该只使用pippython(而不是pip3和{{1}分别)而virtualenv是活跃的。例如:

python3

而不是:

(venv) user my_projectMain $ pip install django-bootstrap3

我的问题编辑显示我为我的目录结构找到了正确的WSGIDaemonProcess python-path。感谢@spectras的帮助。

接下来是一些文件系统权限设置步骤,我在我所关注的指南中完全掩盖了这些步骤(链接如下)。

在完成所有这些之后,我仍然得到:(venv) user my_projectMain $ pip3 install django-bootstrap3 。如果我从settings.py中的INSTALLED_APPS中删除了该行,那么该网站将正确加载。

最终,这是因为我使用的是针对Python 3.4编译的mod_wsgi,在我的Python 3.5.2 virtualenv中,正如上面提到的@Graham Dumpleton。我一直在使用:

安装mod_wsgi
ImportError: No module named 'django_static_jquery'

使用我的操作系统附带的Python 3.4(raspbian jessie,btw)。

但是,由于我手动安装了Python 3.5.2,我还需要手动安装mod_wsgi并根据我的3.5.2安装进行编译。这就是我的所作所为:

sudo apt-get install libapache2-mod-wsgi-py3

在此之后,apache不会重启,因为。我发现这是因为我必须在apache中激活这个新安装的mod_wsgi模块。这是通过以下方式完成的:

mkdir ~/mod_wsgiPy3.5
cd ~/mod_wsgiPy3.5
wget https://pypi.python.org/packages/84/65/44a10ac5e117abc40078575a3dcf3256545ba20495b7330ba59045ec3503/mod_wsgi-4.5.9.tar.gz
tar -zxvf mod_wsgi-4.5.9.tar.gz
cd mod_wsgi-4.5.9/
./configure --with-python=/usr/local/bin/python3
make
sudo make install

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

sudo a2enmod
wsgi
sudo systemctl restart apache2

最终目录结构(〜/ my_projectMain /):

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

现在一切都很好。我推荐这本指南,因为它帮助了我很多:How To Serve Django Applications with Apache and mod_wsgi on Debian 8。看起来他们有一些用于各种操作系统

答案 1 :(得分: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。包括上面的一行。