PythonAnywhere WSGI错误

时间:2017-03-04 21:46:11

标签: django wsgi pythonanywhere

尽管我在过去几周里做出了最大努力,但在尝试建立django框架时,我仍然坚持在Pythonanywhere上部署WSGI文件。这里有类似的主题,但没有一个可以用于我的案例。

我跟随this tutorial并收到以下错误消息:

2017-03-04 22:28:05,284 :Error running WSGI application
2017-03-04 22:28:05,285 :ImportError: No module named 'mysite'
2017-03-04 22:28:05,285 :  File "/var/www/iamcsongor_pythonanywhere_com_wsgi.py", line 11, in <module>
2017-03-04 22:28:05,285 :    application = get_wsgi_application()
2017-03-04 22:28:05,285 :
2017-03-04 22:28:05,285 :  File "/home/iamcsongor/my-first-blog/myvenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2017-03-04 22:28:05,285 :    django.setup(set_prefix=False)
2017-03-04 22:28:05,285 :
2017-03-04 22:28:05,285 :  File "/home/iamcsongor/my-first-blog/myvenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
2017-03-04 22:28:05,286 :    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2017-03-04 22:28:05,286 :
2017-03-04 22:28:05,286 :  File "/home/iamcsongor/my-first-blog/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
2017-03-04 22:28:05,286 :    self._setup(name)
2017-03-04 22:28:05,286 :
2017-03-04 22:28:05,286 :  File "/home/iamcsongor/my-first-blog/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
2017-03-04 22:28:05,287 :    self._wrapped = Settings(settings_module)
2017-03-04 22:28:05,287 :
2017-03-04 22:28:05,287 :  File "/home/iamcsongor/my-first-blog/myvenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
2017-03-04 22:28:05,287 :    mod = importlib.import_module(self.SETTINGS_MODULE)

这是我的WSGI文件:

import os

import sys

path = '/home/iamcsongor/my-first-blog/firstweb'  # use your own PythonAnywhere username here
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())

我已经采取了一些screenshots of the codeGit Repo,如果你能提供帮助,我会感激不尽。感谢

2 个答案:

答案 0 :(得分:4)

如错误所示,您的DJANGO_SETTINGS_MODULE环境变量设置为“mysite”,但该目录不存在。您的设置文件实际上显示为“firstweb”。

答案 1 :(得分:0)

关注

首先将环境变量保存到项目文件夹中的.env文件中 您可以在Bash控制台中运行类似的操作,也可以使用“文件”标签直接编辑.env文件:

cd ~/my-project-dir

echo "export SECRET_KEY=sekritvalue" >> .env

echo "export OTHER_SECRET=somethingelse" >> .env

将python-dotenv安装到您的virtualenv中 工作在my-virtualenv名称上

pip install python-dotenv

,或者,如果您不使用virtualenv:

pip3.6 install --user python-dotenv

,如果要使用的话,可以将其添加到您的requirements.txt中:

echo python-dotenv >> requirements.txt

以及在get_wsgi_application()之前的wsgi文件中

import os

from dotenv import load_dotenv

project_folder = os.path.expanduser('~/my-project-dir')  # adjust as appropriate

load_dotenv(os.path.join(project_folder, '.env'))

并在setting.py文件中进行更改,并删除密钥并放入

import os

SECRET_KEY = os.getenv("SECRET_KEY")

最后,在bash控制台中完成

set -a; source ~/my-project-dir/.env; set +a

echo 'set -a; source ~/my-project-dir/.env; set +a' >> ~/.virtualenvs/my-project-virtualenv/bin/postactivate

所有操作均已删除与wsgi相关的错误