in spite of my best efforts in the past weeks I've been stuck at deploying the WSGI file on Pythonanywhere while trying to set up a django framework. There are similar topics on here, but none were adoptable to my case.
I'm following this tutorial and I get the following error message:
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)
Here's my WSGI file:
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())
I've taken a few screenshots of the code and Git Repo, if you could help, I'd be grateful. Thanks
As the error says, your DJANGO_SETTINGS_MODULE environment variable is set to "mysite", but that directory does not exist. Your settings file actually appears to be in "firstweb".
follow it
Start by saving your environment variables into a .env file in your project folder
You can run something like this in a Bash console, or edit the .env file directly using our "Files" tab:
cd ~/my-project-dir
echo "export SECRET_KEY=sekritvalue" >> .env
echo "export OTHER_SECRET=somethingelse" >> .env
Install python-dotenv into your virtualenv
workon my-virtualenv-name
pip install python-dotenv
or, if you're not using a virtualenv:
pip3.6 install --user python-dotenv
and, optionally, add it to your requirements.txt, if you're using one:
echo python-dotenv >> requirements.txt
and in wsgi file before get_wsgi_application()
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'))
and make changes in setting.py file and remove secret key and put
import os
SECRET_KEY = os.getenv("SECRET_KEY")
and at last, do in bash console
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
all done remove wsgi related error
Related
I am trying to host my local web app on pythonanywhere. [Beginner to Django and web-development]
I am facing the following error while launching the app:
Error running WSGI application
2021-04-26 21:46:32,020: ModuleNotFoundError: No module named 'myapp'
2021-04-26 21:46:32,020: File "/var/www/username_pythonanywhere_com_wsgi.py", line 16, in <module>
2021-04-26 21:46:32,020: application = get_wsgi_application()
2021-04-26 21:46:32,020:
2021-04-26 21:46:32,020: File "/home/username/.virtualenvs/web-virtualenv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2021-04-26 21:46:32,020: django.setup(set_prefix=False)
2021-04-26 21:46:32,020:
2021-04-26 21:46:32,020: File "/home/username/.virtualenvs/web-virtualenv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2021-04-26 21:46:32,021: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2021-04-26 21:46:32,021:
2021-04-26 21:46:32,021: File "/home/username/.virtualenvs/web-virtualenv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
2021-04-26 21:46:32,021: self._setup(name)
2021-04-26 21:46:32,021:
2021-04-26 21:46:32,021: File "/home/username/.virtualenvs/web-virtualenv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
2021-04-26 21:46:32,021: self._wrapped = Settings(settings_module)
2021-04-26 21:46:32,021:
2021-04-26 21:46:32,021: File "/home/username/.virtualenvs/web-virtualenv/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
2021-04-26 21:46:32,021: mod = importlib.import_module(self.SETTINGS_MODULE)
File locations:
The settings.py file is located at:
/home/username/WebDev/Website/Django/MyApp/myapp/myapp
I have modified '/var/www/username_pythonanywhere_com_wsgi.py' file as:
"""
WSGI config for myapp project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""
import os
import sys
sys.path.append("/home/username/WebDev/Website/Django/MyApp/myapp")
os.environ('DJANGO_SETTINGS_MODULE', 'myapp.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I have looked at the similar question on stackoverflow but still cant figure out what is wrong at my end.
Any pointers/help would be appreciated.
From taking a look it looks like the folder structure is fine.
What you might need to do is add the module into your INSTALLED_APPS in settings.
Or, if you're getting an import error or a module not found error for something that you believe is installed, then you've probably installed it into a version of Python or virtualenv that your code is not running in. If it's part of your code that is not being found, then your Python path is not set up so that it sees the module you're trying to import. See https://help.pythonanywhere.com/pages/DebuggingImportError/
The error is resolved.
The problem was that environ is not a function but a process parameter and hence, using it like below was the cause of the error.
os.environ('DJANGO_SETTINGS_MODULE', 'myapp.settings')
I changed the code to:
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
and the error was solved.
I am following this tutorial to setup a Django-gunicorn-nginx server in AWS EC2. After installing all dependancies and making a change in wsgi.py as follows
import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('/home/ubuntu/project/ToDo-application/')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/ubuntu/.local/lib/python3.6/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "todo_app.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I run gunicorn todo_app.wsgi and get the following error:
ubuntu#ip-172-31-61-163:~/project/ToDo-application$ gunicorn todo_app.wsgi
[2018-11-07 11:25:35 +0000] [8211] [INFO] Starting gunicorn 19.7.1
[2018-11-07 11:25:35 +0000] [8211] [INFO] Listening at: http://127.0.0.1:8000 (8211)
[2018-11-07 11:25:35 +0000] [8211] [INFO] Using worker: sync
[2018-11-07 11:25:35 +0000] [8215] [INFO] Booting worker with pid: 8215
[2018-11-07 11:25:35 +0000] [8215] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 578, in spawn_worker
worker.init_process()
File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 135, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 377, in import_app
__import__(module)
File "/home/ubuntu/urbanpiper/ToDo-application/todo_app/wsgi.py", line 20, in <module>
from django.core.wsgi import get_wsgi_application
File "/home/ubuntu/.local/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
File "/home/ubuntu/.local/lib/python3.6/site-packages/django/utils/version.py", line 71, in <module>
#functools.lru_cache()
AttributeError: 'module' object has no attribute 'lru_cache'
Is this because of gunicorn having python2 dependancies and Django being on python3? I tried uninstalling gunicorn and trying it again but it did not work.
# WRONG:
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/ubuntu/.local/lib/python3.6/site-packages')
You ought to create a virutalenv for each uwsgi application you wish to host on the server, rather than setting the virtualenv to the path above. If you followed the linked tutorial word-by-word, then this is the part which needs more explaining:
Make a virtualenv and install your pip requirements
Essentially:
# install virtualenv3
sudo apt-get install virtualenv3
# create the virtual environment, specifically for the stated python version
virtualenv -p python3.6 TITLE_OF_VENV
# You now have a directory called TITLE_OF_VENV (You may wish to replace this
# with something more subtle).
# Activate the virtualenv for your current shell session
. TITLE_OF_VENV/bin/activate
# The dot above is intentional and is a quick way to write source, which
# imports the environment vars
Your shell prompt should now look like this: (TITLE_OF_VENV) ubuntu#ip-172-31-61-163:~/project/ToDo-application$ indicating that the venv is active. To switch out of the venv run the command deactivate.
Anything which you install with pip here will then live in the directory TITLE_OF_VENV/python3.6/site-packages (while this virutal environment is active). This has the advantage of keeping different project requirements separate.
Test the python version (with the venv still active):
(TITLE_OF_VENV)$ python --version
Python 3.6
Now install gunicorn into this virtual environment, along with any other project requirements:
(TITLE_OF_VENV)$ pip install gunicorn
(TITLE_OF_VENV)$ pip install -r requirements.txt
Update your uwsgi.py:
import os
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "todo_app.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And then launch it from within the virtual environment:
(TITLE_OF_VENV)$ gunicorn todo_app.wsgi:application
You could add the -D flag to the gunicorn command also, which makes it run in the background. Also don't make this server publicly accessible. If it's a production box, you need to run it behind nginx!
I am trying to deploy my django application on the pythonanywhere. I have install all the packages that requires for the application. I am also using django_select2 reusable app in my application.
Firstly, i activate virtual environment and install the django_select2 by this command :
$ pip install django_select2
When i execute below command
$ pip freeze
cryptography==2.2.2
Django==2.0.7
django-appconf==1.0.2
django-select2==6.1.0
Flask==1.0.2
Flask-JWT==0.3.2
Flask-SQLAlchemy==2.3.2
furl==1.2
idna==2.7
As you see django_select2 is also in the installed list. I also check site-packages inside my virual environment. There is also django_select2 folder.
However when i run my app it generates the ImportError: No module named 'django_select2' inside my error.log
Here is the part of my error.log
2018-07-14 14:18:53,934: Error running WSGI application
2018-07-14 14:18:53,934: ImportError: No module named 'django_select2'
2018-07-14 14:18:53,934: File "/var/www/harunergul_pythonanywhere_com_wsgi.py", line 22, in <module>
2018-07-14 14:18:53,935: application = get_wsgi_application()
2018-07-14 14:18:53,935:
2018-07-14 14:18:53,935: File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2018-07-14 14:18:53,935: django.setup(set_prefix=False)
2018-07-14 14:18:53,935:
2018-07-14 14:18:53,935: File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
2018-07-14 14:18:53,935: apps.populate(settings.INSTALLED_APPS)
2018-07-14 14:18:53,935:
2018-07-14 14:18:53,935: File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 85, in populate
2018-07-14 14:18:53,936: app_config = AppConfig.create(entry)
2018-07-14 14:18:53,936:
2018-07-14 14:18:53,936: File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 90, in create
2018-07-14 14:18:53,936: module = import_module(entry)
Any suggestions? What i have missing?
The problem is application is not using virtual environment in this case. If we look at the below line we will see /usr/local/... . So the application is using pythonanywhere.com default environment.
File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13,
which means whatever we install in our virtualenv has no effect on default server. In pythonanywhere.com there is virtualenv menu, be sure if you input correct path info in this menu.
The default pip on PythonAnywhere is for Python 2.7, so you've been installing your modules into the wrong version of Python (your web app is using 3.5). Use pip3.5 to install your modules.
I'm trying to intall the asterisk_click2dial module on ODOO and this error comes to me in the log file:
ValueError: Routing: posting a message without model should be with a parent_id (private mesage).
2015-03-09 15:23:38,262 11093 ERROR ? werkzeug: Error on request:
Traceback (most recent call last):
File "/home/odoo/odoo/lib/python2.7/site-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/home/odoo/odoo/lib/python2.7/site-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/opt/odoo/openerp/service/server.py", line 281, in app
return self.app(e, s)
File "/opt/odoo/openerp/service/wsgi_server.py", line 216, in application
return application_unproxied(environ, start_response)
File "/opt/odoo/openerp/service/wsgi_server.py", line 202, in application_unproxied
result = handler(environ, start_response)
File "/opt/odoo/openerp/http.py", line 1274, in __call__
self.load_addons()
File "/opt/odoo/openerp/http.py", line 1293, in load_addons
m = __import__('openerp.addons.' + module)
File "/opt/odoo/openerp/modules/module.py", line 79, in load_module
mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
File "/opt/odoo/addons/base_phone/__init__.py", line 23, in <module>
from . import wizard
File "/opt/odoo/addons/base_phone/wizard/__init__.py", line 23, in <module>
from . import number_not_found
File "/opt/odoo/addons/base_phone/wizard/number_not_found.py", line 25, in <module>
import phonenumbers
ImportError: No module named phonenumbers
The problem is just that I installed that module (phonenumbers) plus the py-Asterisk module without errors using pip install phonenumbers and pip install py-Asterisk and the error persists.
I noticed I have at least two versions of python installed (2.6 and 2.7) but both modules are installed at the same version from odoo (I can see the modules in the python2.7 cli when, for example, I write phonenumbers or search).
Has anybody any idea what is happening to me? I'd be gratefull for some specific response. Thanks.
Here the connector's page: OpenERP - Asterisk connector
Ok, my bad. During odoo instalation I created a virtual environment under the Odoo system user account which was used solely by the Odoo server so I just need to install these modules under that enviorment. This works for me:
First let’s switch from root to the odoo user, then create a new virtual environment called odoo and activate it:
su - odoo
/usr/local/bin/virtualenv --python=/usr/local/bin/python2.7 odoo
source odoo/bin/activate
(If you have the virtual environment created like me just ignore the second line). Before starting the module installation we need to add the path to the PostgreSQL binaries, otherwise the PsycoPG2 module install will fail (I ignored this one too):
export PATH=/usr/pgsql-9.3/bin:$PATH
Then I can do pip install... Thank you all for your help.
I have done everything said on the following page.
https://help.ubuntu.com/community/Django
but it gives me an error page.
MOD_PYTHON ERROR
ProcessId: 2220 Interpreter:
'localhost.localdomain'
ServerName:
'localhost.localdomain' DocumentRoot:
'/etc/apache2/htdocs'
URI: '/' Location:
'/' Directory: None Filename:
'/etc/apache2/htdocs' PathInfo:
'/'
Phase: 'PythonHandler'
Handler:
'django.core.handlers.modpython'
Traceback (most recent call last):
File
"/usr/lib/python2.6/dist-packages/mod_python/importer.py",
line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File
"/usr/lib/python2.6/dist-packages/mod_python/importer.py",
line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File
"/usr/lib/python2.6/dist-packages/mod_python/importer.py",
line 1128, in _execute_target
result = object(arg)
File
"/usr/lib/pymodules/python2.6/django/core/handlers/modpython.py",
line 228, in handler
return ModPythonHandler()(req)
File
"/usr/lib/pymodules/python2.6/django/core/handlers/modpython.py",
line 191, in call
self.load_middleware()
File
"/usr/lib/pymodules/python2.6/django/core/handlers/base.py",
line 33, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File
"/usr/lib/pymodules/python2.6/django/utils/functional.py",
line 276, in getattr
self._setup()
File
"/usr/lib/pymodules/python2.6/django/conf/init.py",
line 40, in _setup
self._wrapped = Settings(settings_module)
File
"/usr/lib/pymodules/python2.6/django/conf/init.py",
line 75, in init
raise ImportError("Could not import settings '%s' (Is it on
sys.path? Does it have syntax
errors?): %s" % (self.SETTINGS_MODULE,
e))
ImportError: Could not import settings
'examples.settings' (Is it on sys.path? Does it have syntax errors?): No module named examples.settings
what could be wrong?
complete noob here.
If you are deploying django project for production under ubuntu server it is recommended to use WSGI. Just fallow the django documentation: https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/
Installing apache with wsgi under ubuntu is easy:
apt-get install libapache2-mod-wsgi
In case you just want to explore django you do not need to setup apache or anything. Just use manage.py runserver instead.