Django + mod_wsgi + apache2:ImportError:没有名为<project>

时间:2016-10-21 09:18:24

标签: python django apache mod-wsgi

我正试图在我的django项目上运行任何“migrate”命令时遇到问题的根源。 我在虚拟环境中部署了一个django应用程序,并且使用mod_wsgi和apache2,该应用程序在www.mysite.com/test/simulatore上正确提供。 当我编辑模型时出现问题,所以我必须运行

python manage.py makemigrations

经过一些初步的故障排除后,我想我已经在wsgi.py上解决了问题 如果我尝试(从我的virtualenv)尝试运行命令

python -i /home/carma/mycarma_test/mycarma/mycarma/wsgi.py

这是结果

['/home/carma/mycarma_test/mycarma/mycarma', '/home/carma/mycarma/testenv/lib/python35.zip', '/home/carma/mycarma/testenv/lib/python3.5', '/home/carma/mycarma/testenv/lib/python3.5/plat-x86_64-linux-gnu', '/home/carma/mycarma/testenv/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/carma/mycarma/testenv/lib/python3.5/site-packages', '/home/carma/mycarma_test/mycarma/mycarma/..', '/home/carma/mycarma_test', '/home/carma/mycarma_test/mycarma']
Traceback (most recent call last):
  File "/home/carma/mycarma_test/mycarma/mycarma/wsgi.py", line 22, in <module>
    application = get_wsgi_application()
  File "/home/carma/mycarma/testenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/carma/mycarma/testenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/carma/mycarma/testenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/carma/mycarma/testenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/carma/mycarma/testenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/carma/mycarma/testenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named '”mycarma'

正如您所见,mycarma(项目名称)模块报告时带有单引号,只是初始双引号

ImportError: No module named '”mycarma'

这告诉我(我认为)某个地方存在配置问题,传递了错误的参数,但我无法找到。以下是我的设置:

目录结构

mycarma_test
└── mycarma
├── manage.py
├── mycarma
│   ├── development_settings.py
│   ├── __init__.py
│   ├── settings.py
│   ├── test_settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements
│   ├── common.txt
│   ├── dev.txt
│   ├── prod.txt
│   └── test.txt
├── simulatore
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── templates
    ├── base.html
    ├── registration
    │   └── login.html
    └── simulatore
        └── index.html

mycarma / mycarma中的文件wsgi

import os
import sys

from django.core.wsgi import get_wsgi_application

DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
sys.path.append('/home/carma/mycarma_test')
sys.path.append('/home/carma/mycarma_test/mycarma')
print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mycarma.settings")

application = get_wsgi_application()

请注意我添加了两个附加和打印以便测试/排除故障。但也没有他们没有运气。 的 settings.py

ROOT_URLCONF = 'mycarma.urls'
STATIC_URL = '/static/'
STATIC_ROOT = '/home/carma/mycarma/static'

Apache2配置文件

        <Directory /home/carma/mycarma/static>
            Require all granted
    </Directory>

    WSGIDaemonProcess pydaemon-1 processes=1 threads=5 python-path=/home/carma/mycarma/ python-home=/home/carma/mycarma/mycarmaenv
    WSGIDaemonProcess pydaemon-2 processes=1 threads=5 python-path=/home/carma/mycarma_test/mycarma/ python-home=/home/carma/mycarma/testenv

    WSGIScriptAlias /simulatore /home/carma/mycarma/mycarma/wsgi.py

    <Location /simulatore>
            WSGIProcessGroup pydaemon-1
            WSGIApplicationGroup %{GLOBAL}
    </Location>
    <Directory /home/carma/mycarma/mycarma>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

    WSGIScriptAlias /test /home/carma/mycarma_test/mycarma/mycarma/wsgi.py

    <Location /test>
            WSGIProcessGroup pydaemon-2
            WSGIApplicationGroup %{GLOBAL}
    </Location>
    <Directory /home/carma/mycarma_test/mycarma/mycarma>
            <Files wsgi.py>
                    Require all granted
            </Files>
    </Directory>

感谢。

编辑:发现问题 在dev bin / activate脚本中

DJANGO_SETTINGS_MODULE=”carma.test_settings”

使用了一些奇怪的双引号。修改过,一切正常。

1 个答案:

答案 0 :(得分:2)

我很确定这是因为你在$ ping https://github.com ping: cannot resolve https://github.com: Unknown host $ ping github.com PING github.com (192.30.253.113): 56 data bytes 64 bytes from 192.30.253.113: icmp_seq=0 ttl=43 time=110.597 ms django配置中有额外的双引号。这种情况发生在django bootstraps并试图加载你的所有应用程序时它无法找到它。