我在 StackOverflow(主要针对 Django 1.4)上看到过其他人对此发表的文章,但这些都没有为我提供成功的解决方案。
我无法使用 Apache 正确设置 Django 1.5.1。我收到以下错误:
“ImportError:无法导入设置 'foobar.settings'(它在 sys.path 上吗?):没有名为 foobar.settings 的模块”
在 virtualbox 上使用全新安装的 Xubuntu 13.04。
这是我的设置脚本
$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi
$ sudo apt-get install python-setuptools
$ sudo apt-get install build-essential nmap python-dev
$ sudo easy_install pip
$ sudo pip install virtualenvwrapper
$ cd ~
$ vim .bashrc
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/foobar.com
source /usr/local/bin/virtualenvwrapper.sh
# save .bashrc
$ . .bashrc
$ mkdir -p foobar.com/prod
$ cd foobar.com/prod
# start a python project with virtualenv
$ mkvirtualenv foobar --no-site-packages
$ workon foobar
$ pip install django
# this will create project root
$ django-admin.py startproject foobar
vim foobar/foobar/settings.py
# add database settings to settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/banjo/foobar.com/prod/foobar/foobar/foobar.db'
}
}
# save settings.py
$ cd /home/banjo/foobar.com/prod/foobar
python manage.py syncdb
# create directory for static files
$ mkdir static
# create directory for apache log files
$ cd /home/banjo/foobar.com/prod
$ mkdir logs
$ pip freeze > requirements.txt
# create apache .conf file for site
$ sudo vim /etc/apache2/sites-available/foobar.conf
<VirtualHost *:80>
ServerAdmin banjo@foobar.com
ServerName foobar
ServerAlias foobar
Alias /static/ /home/banjo/foobar.com/prod/foobar/static/
<Directory /home/banjo/foobar.com/prod/foobar/static>
Order allow,deny
Allow from all
</Directory>
LogLevel warn
ErrorLog /home/banjo/foobar.com/prod/logs/apache_error.log
CustomLog /home/banjo/foobar.com/prod/logs/apache_access.log combined
WSGIDaemonProcess foobar threads=15 display-name=%{GROUP} python-path=/home/banjo/foobar.com/prod/foobar/foobar:/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages
WSGIProcessGroup foobar
WSGIScriptAlias / /home/banjo/foobar.com/prod/foobar/foobar/wsgi.py
</VirtualHost>
# save .conf file
# add site to /etc/hosts
$ vim /etc/hosts
127.0.0.1 foobar
# save hosts file
# configure .wsgi file and the following
$ vim /home/banjo/foobar.com/prod/foobar/foobar/wsgi.py
import sys
path = '/home/banjo/foobar.com/prod/foobar/foobar'
if path not in sys.path:
sys.path.insert(0, '/home/banjo/foobar.com/prod/foobar/foobar')
# save wsgi.py
$ sudo a2ensite foobar.conf
$ sudo service apache2 restart
# open browser and navigate to site
http://foobar
这是我的目录树
+-- foobar.com
¦ +-- prod
¦ +-- foobar
¦ ¦ +-- foobar
¦ ¦ ¦ +-- foobar.db
¦ ¦ ¦ +-- __init__.py
¦ ¦ ¦ +-- __init__.pyc
¦ ¦ ¦ +-- settings.py
¦ ¦ ¦ +-- settings.pyc
¦ ¦ ¦ +-- urls.py
¦ ¦ ¦ +-- wsgi.py
¦ ¦ +-- manage.py
¦ ¦ +-- static
¦ +-- logs
¦ ¦ +-- apache_access.log
¦ ¦ +-- apache_error.log
¦ +-- requirements.txt
这是完整的错误日志
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] mod_wsgi (pid=3424): Exception occurred processing WSGI script '/home/banjo/foobar.com/prod/foobar/foobar/wsgi.py'.
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in call
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] self.load_middleware()
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 53, in getattr
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] self._setup(name)
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 48, in _setup
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] self._wrapped = Settings(settings_module)
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 134, in init
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] ImportError: Could not import settings 'foobar.settings' (Is it on sys.path?): No module named foobar.settings
这是我的系统路径的输出
(foobar)banjo@sandbox:~/foobar.com$ python
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for path in sys.path: print path
...
/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7
/home/banjo/.virtualenvs/foobar/lib/python2.7/plat-i386-linux-gnu
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-tk
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-old
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-i386-linux-gnu
/usr/lib/python2.7/lib-tk
/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages