Error running wsgi application: ModuleNotFoundError - django

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.

Related

ModuleNotFoundError: No module named 'webpush'

I am using webpush in my Django application. Following the recommendation it works well locally.
When I push the code to my host, it gives me this mistake ModuleNotFoundError: No module named 'webpush'.
Application does not work.
Error log:
2023-02-15 07:51:15,780: Error running WSGI application
2023-02-15 07:51:15,780: ModuleNotFoundError: No module named 'webpush'
2023-02-15 07:51:15,780: File "/var/www/www_……….._pl_wsgi.py", line 30, in
2023-02-15 07:51:15,781: application = get_wsgi_application()
2023-02-15 07:51:15,781:
2023-02-15 07:51:15,781: File "/home/……/.virtualenvs/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-02-15 07:51:15,781: django.setup(set_prefix=False)
2023-02-15 07:51:15,781:
2023-02-15 07:51:15,782: File "/home/……/.virtualenvs/venv/lib/python3.10/site-packages/django/init.py", line 24, in setup
2023-02-15 07:51:15,782: apps.populate(settings.INSTALLED_APPS)
2023-02-15 07:51:15,782:
2023-02-15 07:51:15,782: File "/home/……./.virtualenvs/venv/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
2023-02-15 07:51:15,782: app_config = AppConfig.create(entry)
2023-02-15 07:51:15,782:
2023-02-15 07:51:15,782: File "/home/…../.virtualenvs/venv/lib/python3.10/site-packages/django/apps/config.py", line 193, in create
2023-02-15 07:51:15,782: import_module(entry)
2023-02-15 07:51:15,783:
I did « pip install Django-webpush » (no mistake), I executed « python manage.py migrate » (in line with instructions on host as well, no mistake at this point, migration is done successfully).
I have the Error when I try to reload the application on host.
Anyone can help?

ImportError: No module named 'django_select2'

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.

PythonAnywhere WSGI error

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

Django Gunicorn ImportError: No module named django.core.wsgi

I have created a Django application but now have plans to use some asynchronous (real-time) functionality in some areas of the site. After doing some research I think I should use gevent-socketio and therefore it is required I switch the application server to Gunicorn.
I have fallen at the first hurdle of deploying Gunicorn, I have installed with the command sudo apt-get install gunicorn and try to run my application with gunicorn project.wsgi:application but it fails and produces the following error:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 473, in spawn_worker
worker.init_process()
File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 100, in init_process
self.wsgi = self.app.wsgi()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 115, in wsgi
self.callable = self.load()
File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 33, in load
return util.import_app(self.app_uri)
File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 362, in import_app
__import__(module)
File "/home/alex/django_projects/fantasymatchday_1/fantasymatchday_1/wsgi.py", line 13, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
2014-11-20 17:31:45 [6605] [INFO] Worker exiting (pid: 6605)
2014-11-20 17:31:45 [6600] [INFO] Shutting down: Master
2014-11-20 17:31:45 [6600] [INFO] Reason: Worker failed to boot.
Can anybody give me a clue to what I need to do from here?
I am using python 3.4.0 and Django 1.6
You have to install django and gunicorn in the same environment.
If you use virtualenv make sure you have both in the same virtual environment.

mod_wsgi + python 2.7.5 import error

I have installed and probably right configurated apache2, mod_wsgi and python 2.7.5. The problem is that 127.0.0.1 is not opening (loading is just freezed) and I see following log in error.log:
[Wed Jun 19 14:49:11 2013] [notice] Apache/2.2.22 (Ubuntu) mod_wsgi/3.4 Python/2.7.3 configured -- resuming normal operations
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site.py", line 548, in <module>
main()
File "/usr/local/lib/python2.7/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/lib/python2.7/site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/lib/python2.7/site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/lib/python2.7/site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "/usr/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
import re
File "/usr/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Tried to restart apache, but it's not working. Thanks for any help!
Just found solution. I checked 'sys.prefix' in my python's interpretator and it printed '/usr/local', but I had not set WSGIPythonHome in apache config (as written in mod_wsgi docs for this case). And just for experiment I set 'WSGIPythonHome /usr'. It's strange, but it resolves my problem, everything is fine now!