我正在使用Apache2部署django站点。当我尝试执行manage.py时,它会转到
ImportError: Could not import settings 'mysite.settings' No module named mysite.settings
这是我的项目结构:
healthtweets/
manage.py
code/
__init__.py
health_tweets/
wsgi.py
settings.py
models.py
__init__.py
我试图添加所有可能的python路径,这是我的wsgi.py: 进口口 导入系统 来自网站导入additedir 来自django.core.handlers.wsgi导入WSGIHandler
sys.path.insert(0,'/home/usr/webapps/healthtweets_dev/healthtweets/')
sys.path.append('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets')
sys.path.append('/home/usr/webapps/healthtweets_dev/healthtweets/code/')
root = os.path.join(os.path.dirname(__file__), '..')
sys.path.insert(0, root)
os.environ['DJANGO_SETTINGS_MODULE'] = 'health_tweets.settings'
#application = django.core.handlers.wsgi.WSGIHandler()
application = WSGIHandler()
这是当前的python路径:
['/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/..',
'/home/usr/webapps/healthtweets_dev/healthtweets/',
'/home/usr/webapps/healthtweets_dev/lib/python2.7/python_memcached-1.48-py2.7.egg',
'/home/usr/webapps/healthtweets_dev/healthtweets/code',
'/home/usr/webapps/healthtweets_dev/lib/python2.7',
'/home/usr/webapps/django/lib/python2.7/python_memcached-1.48-py2.7.egg',
'/home/usr/lib/python2.7/pymongo-2.3-py2.7-linux-x86_64.egg',
'/home/usr/lib/python2.7/django_picklefield-0.2.1-py2.7.egg',
'/home/usr/webapps/django/lib/python2.7/python_memcached-1.48-py2.7.egg',
'/home/usr/lib/python2.7',
'/home/usr/webapps/healthtweets_dev/healthtweets/manage.py',
'/home/usr',
'/home/usr/webapps/django/lib/python2.7',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/PIL',
'/home/usr/webapps/healthtweets_dev/healthtweets/manage.py',
'/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets',
'/home/usr/webapps/healthtweets_dev/healthtweets/code/']
这是httpd.conf:
ServerRoot "/home/usr/webapps/healthtweets_dev/apache2"
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule wsgi_module modules/mod_wsgi.so
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/usr/logs/user/access_healthtweets_dev.log combined
ErrorLog /home/usr/logs/user/error_healthtweets_dev.log
KeepAlive Off
Listen 31846
MaxSpareThreads 3
MinSpareThreads 1
ServerLimit 1
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
WSGIPythonPath /home/usr/webapps/healthtweets_dev/healthtweets/code/:/home/usr/webapps/healthtweets_dev/lib/python2.7
WSGIDaemonProcess healthtweets_dev processes=2 threads=12 python-path= /home/usr/webapps/healthtweets_dev/healthtweets/code/:/home/usr/webapps/healthtweets _dev/lib/python2.7
WSGIProcessGroup healthtweets_dev
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/wsgi.py
除此之外,我还有chmod 711到项目目录。但没有任何作用。 非常感谢你!
编辑: 我测试了这篇文章中描述的路径:How to troubleshoot - ImportError: Could not import settings 'mysite.settings' when deploying django? 他们都存在......
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/health_tweets/settings.py')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/code/')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/healthtweets/')
True
>>> os.path.exists('/home/usr/webapps/healthtweets_dev/')
True
但是最后的测试没有通过:
>>> import health_tweets.settings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named health_tweets.settings
答案 0 :(得分:1)
您的目录布局格式错误,因此很难分辨出您的设置位于healthtweets
或healthtweets/code/health_tweets
中的位置,从您的评论中我认为它是第二个。如果是这样,您需要在/home/usr/webapps/healthtweets_dev/healthtweets/
(最好是第一个)中设置sys.path
,并将DJANGO_SETTINGS_MODULE
设为code.health_tweets.settings
,或设为{{1}在/home/usr/webapps/healthtweets_dev/healthtweets/code
中,将sys.path
设为DJANGO_SETTINGS_MODULE
。
如果以上解决方案都不起作用,您可以
health_tweets.settings
中有一些其他code.health_tweets
(第一个案例)或health_tweets
(第二个案例)模块或包,遮蔽您期待的那个或一般规则是sys.path
应该是一个Python限定(子)模块路径,如果这个Python限定路径必须在你的{{}>目录包含根目录{1}}。
在DJANGO_SETTINGS_MODULE
脚本中添加sys.path
加上 WSGIPythonPath
加上中的WSGIDaemonProcess --python-path
内容只会添加内容增加混乱。