Error running WSGI application, ModuleNotFoundError: No module named 'mysite'. Also not able to find Static files - django

I'm trying to deploy my website using pythonanywhere. I've given the correct path in wsgi file which is as follows (see code below). Still I'm getting a ModuleNotFoundError.
Also, it's not able to find Static files, however, they are at correct location.
Wsgi file code:
{
path = '/home/divyanshu964/portfolio/Personal_portfolio/'
if path not in sys.path:
sys.path.insert(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'Personal_portfolio.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
}
Can someone help? Thanks.

Hello #DivyanshuRaghuwanshi to load static files you can set DEBUG=True inside settings.py but it's not a good practice to set DEBUG on in production so another way is to set your DEBUG=False and create STATIC_ROOT and configure it inside project root urls.py and than run python manage.py collectstatic command and than give path of your static root directory in pythonanywhere configuration

Apart from settings directly related to Django, there are some extra steps on PythonAnywhere.
wsgi file: you need to edit the file that is linked on the Web app page, not a file inside your project (check the docs here)
static files: set up static files mappings on the Web app page as well (check the docs here).

Related

While importing 'myapp.app' an import error was raised

I'm building a flask application, located in a subdirectory within my project called myapp. Running gunicorn --bind 0.0.0.0:$PORT myapp.app:app works fine, no errors, regardless of what FLASK_APP is set to. When I try to use Flask's CLI, however, it fails to find my app (reasonable), but when I set FLASK_APP to myapp.app., it appears to be doubling up the import path, resulting in an import error:
FLASK_APP=myapp.app flask run
* Serving Flask app 'myapp.app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'myapp.myapp.app', an ImportError was raised.
How can I solve this? Is this a bug in Flask?
There was a __init__.py in my project directory that was causing this.
I dug into the source code of flask in cli.py and found the following code in prepare_import:
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, "__init__.py")):
break
Because I had an __init__.py in my project directory (also called myapp), this made flask try to import myapp.app from the ancestor of my project, resulting in myapp.myapp.app.

Running Gunicorn and Django

I don't know how to use gunicorn with django. Could you give me some help?
This is how I run the server with django. It is https
python3 manage.py runsslserver xx.8x.x3.x4:443 --certificate /etc/letsencrypt/live/callservicesvps.online/fullchain.pem --key /etc/letsencrypt/live/callserv.com/privkey.pem
And in the gunicorn documentation it is mentioned that it must be executed as follows:
gunicorn myproject.wsgi
And here I have 2 questions. What is myproject.wsgi? Where Can I find it? Because if I look in the directory where the django project is, the only thing I find with wsgi is a file called wsgi.py
Running the server as follows gives me an error
gunicorn /home/proyectdirectory/wsgi.py
It also gives me an error if I put:
gunicorn /home/proyectdirectory/wsgi:Some_directory_where_the_proyec_is
What is myproject.wsgi?
myproject.wsgi IS the wsgi.py file you have located inside your project.
[End of answer]
But a further explanation will clear up why this is..
I imagine most people will look atmyproject.wsgi and see a file with an extension file type .wsgi but this is just because of the way importing of modules is written in python.
I want to clarify what a module and a package before continuing an explanation.
What is a Module
From the jargon heavy python docs
A module is a file containing Python definitions and statements.
Put simply, a module in python is just a python file containing any sort of functions, variables, or classes etc.
What is a Package
A package is just a collection of modules. The most simplest example, a special directory containing python files. In order to tell python that a directory is a package it must have a file named __init__.py inside of it. You will find a few of these inside different directories inside your django project. This is why they are there.
Now, I can say what I want to say which is the structure of the module namespace in python.
package.subpackage.module
If you look inside your wsgi.py file you'll see a good example of importing from django's own wsgi module.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')
application = get_wsgi_application()
More specifically
from django.core.wsgi import get_wsgi_application
If you look inside the django package you can see how this works.
django (package) core (subpackage) wsgi (module)
Remember, the command gunicorn myproject.wsgi should be run inside the base directory of your django project. I always remember this as the directory containing the manage.py file. That is how gunicorn can find the wsgi.py file using the module namespace in this way. gunicorn is written in python afterall.
gunicorn /home/proyectdirectory/wsgi.py will error because python module imports don't contain / and even if you tried gunicorn home.proyectdirectory.wsgi home and proyectdirectory are not python packages.
Now hopefully this makes sense:
"If gunicorn myproject.wsgi is referring to the wsgi.py file why not just put gunicorn myproject.wsgi.py?"
You can't put a .py extension because this will refer to a module inside the wsgi subpackage with a filename py.py!

Django deployment, cannot import settings

I'm trying to deploy an application with django.
I've put my django_project directory in /home/XXX/websites/YYY.
The web server root is in /srv/http/YYY and it only contains a directory named static and an apache.wsgi file.
The content of apache.wsgi is as follow:
import os, sys
sys.path.append('/home/XXX/websites/YYY')
os.environ['DJANGO_SETTINGS_MODULE'] = 'YYY.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Whenever I reload apache modules and try to load a page, I got an import error :
ImportError: No module named YYY.settings
I don't understand. Since the settings.py file is in /home/XXX/websites/YYY/YYY/settings.py, why this don't work ?
I've manage to make this work by putting the django project in the web server root directory, but that wasn't the place where I wanted to put it.
Thanks for your help.

Relative path in settings.py in django

for various settings(MEDIA_ROOT,TEMPLATE_DIRS) in setting.py it is instructed to give absolute path.I have configured apache with mod_wsgi.I have a wsgi script in the folder named apache that redirects to settings.py.
import os
import sys
path = 'D:/Projects/Dan'
if path not in sys.path:
sys.path.append(path )
os.environ['DJANGO_SETTINGS_MODULE'] = 'Django.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
(don't misunderstand - Django is the name of my project.)
Okay my question is - now that we have imported the path to my project to system path in the wsgi script, isn't it more feasible that I give a relative path in settings.py,since that would make deploying easier.If I am wrong please tell me the standard procedure so that I can set all the path in one file other than 3 files(Apache - httpd.conf, mod_wsgi - django.wsgi, django - settings.py).
Use:
import os
this_directory = os.path.dirname(__file__)
and then:
absolute_directory = os.path.join(this_directory, 'relative.txt')
BTW, calling your project 'Django' is ill advised given that Django package itself is 'django'. You shouldn't rely on differences in case not causing confusion. In short, Python packages/module names should ever differ just by case. Your site project directory is treated as a Python package and thus the problem.

Setting up Django on Server with WSGI getting Import Error "Module named myproject.urls Not found"

So I have created a django site and I wanted to move it from my computer to my server. I set up Django on the server, and used the WSGI configuration. When I try to go to the home page I get an Import Error, It says that the module "myproject.urls" isn't found. It's a Django error, and it looks like it is getting the settings.py file and looking at the setting for ROOT_URLCONF and seeing the right urls file. I created this project with the usual django-admin.py startproject myproject and I just wanted to see if everything was configured correctly, but now I'm getting this error.
Any Suggestions?
Remove the "myproject" from "myproject.urls". Somehow WSGI addresses the settings as the root, so no need to refer to it again.
It sounds like myproject isn't on your path - what happens if you load up a python shell and run import myproject? If that works, what happens when you run import myproject.urls? If only the second import fails, there's a syntax error in your urls.py or one of the files it imports.
#Afrowave, you saved a huge headache - thank you from me too!
Further to this - I did a little more digging and wanted to avoid having to amend a dozen files in my app to account for loosing 'myproject.' and the start of every import.
Instead, I found if you do something like this -- you don't have to :)
ROOT = '/home/user/path_to_project_root' # In my case, also the dir that contains media, templates etc
APP_ROOT = '/home/user/path_to_django_project'
sys.path.append(ROOT)
sys.path.append(APP_ROOT)
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Hope this helps someone in future.