ImportError on newaly production django + apache + mod_wsgi - django

I'm in the middle of deploying my django project.
got the wsgi working and now I got importError.
error:
Exception Type: ImportError
Exception Value:
No module named forms
this is the import:
from profile.forms import UserRegistrationForm
profile is a folder resides in my project dir.
this is the wsgi script I use:
import os
import sys
path = '/srv/www/project'
if path not in sys.path:
sys.path.append(path)
path = '/srv/www'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

got it to work, needed to add project name as prefix.
from projectname.profile.forms import <something>

Related

No module named 'django' when trying to import Django into Scrapy

This is my folder structure:
root:
|--Django_project
|--|-- db_app
|--|-- Django_project
|--|-- manage.py
|--Scrapy_project
|--|--Scrapy_project
|--|--|--spiders
|--|--|--settings.py
|--|--|--pipelines.py
|--|--|--items.py
|--|--|--middlewares.py
|--|--scrapy.cfg
In settings.py I have this:
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath('.')))
os.environ['DJANGO_SETTINGS_MODULE'] = 'Django_project.settings'
import django
django.setup()
I've tried every possible path, including an absolute path to the project root, to the Django project, to the Django app - nothing works and I get this:
ModuleNotFoundError: No module named 'django'
Thanks for the help!
EDIT:
I should probably clarify that I'm running in a virtual environment.

Django: django.core.exceptions.ImproperlyConfigured: WSGI application 'wsgi.application' could not be loaded; Error importing module

I used to check every answer to this question< but NOTHING helped me.
Here's a error:
edjango.core.exceptions.ImproperlyConfigured: WSGI application 'wsgi.application' could not be loaded; Error importing module.
In settings file is:
WSGI_APPLICATION = 'wsgi.application'
and WSGI file is:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyLittleTest.settings")
application = get_wsgi_application()
Can you help me with this issue plz?
I think you need:
WSGI_APPLICATION = 'mylittletest.wsgi.application'
or how your project is called.

Django Gunicorn no module named error

My project directory structure looks like following:
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = get_wsgi_application()
and my manage.py file:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
in my settings .py I have:
WSGI_APPLICATION = 'config.wsgi.application'
I added my project path to python with following:
export PYTHONPATH=/home/ec2-user/amm_inovision_backend:$PYTHONPATH
and I am trying to run gunicorn command where my manage.py file is as: gunicorn amm_inovision_backend.config.wsgi:application
But it throws me error no module named amm_inovision_backend.config.wsgi
If I run instead gunicorn config.wsgi:application it throws no module named amm_inovision_backend.config.settings
What am I doing wrong?
Note: in the screenshot it is amm_ino_backend but actually it is amm_inovision_backend in the production
when you want to use gunicorn you must run gunicorn installed on your virtual environment.
so you don't need to export any python path, you only need to find path to gunicorn on your virtual-env where Django is installed, or you can add path to virtual environment not to project files.
also you need first to edit your wsgi.py
you have to replace this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") with
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
your command must be something like that:
/path/to/.virtual-envs/your-env/bin/gunicorn config.wsgi:application
Use:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
note the "CONFIG.SETTINGS"

Django Shell in Eclipse not starting

I am getting this error message when I try to run the shell from Eclipse Neon while I can successfully run the Django shell from command window. I am using Python 3.4 and Django 1.10. Any idea where the problem is?
WSGI file:
'''
WSGI config for MyProject 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/1.10/howto/deployment/wsgi/
'''
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")
application = get_wsgi_application()
I beleive This is a known bug in Eclipse Pydev.
https://www.brainwy.com/tracker/PyDev
Fixed for 5.6
Git: 2c8cd03 2017-03-12 Fabio Zadrozny #PyDev-752: Django version not
detected if > 1.10
And a fix has been posted for it.
You could try start a python shell as normal, then:
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os; os.environ['DJANGO_SETTINGS_MODULE'] = '<Project Name>.settings'; import django
sys.path.append(os.path.expanduser('<path to your project>'))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Replace 'Project Name' with your project name and 'path to your project' with your path
alternatively, use a pre 1.10 version of Django i.e 1.7

Serve static files when using the Django WSGI app

I am using the python-reload package. The readme gives this code to use it with django:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
from django.core.management import execute_from_command_line
if 'livereload' in sys.argv:
from django.core.wsgi import get_wsgi_application
from livereload import Server
application = get_wsgi_application()
server = Server(application)
# Add your watch
# server.watch('path/to/file', 'your command')
server.serve()
else:
execute_from_command_line(sys.argv)
However Django doesn't serve staticfiles anymore. The runserver command still works, so I guess it's related to the wsgi app. How can I made django serve static files when using WSGI? Is it posible?