No module named 'mysite.settings' - django

I know this question has been asked quite a lot but all the solutions given do not seem to fit for me.
I run this in a venv with python 3.6.8 and django 2.2.10
When I run django from cli it works and all functionality is working great so I know it is not django itself that is failing me.
Path to wsgi script is "/opt/sites/aws/okta/wsgi.py"
Actual wsgi.py:
import os, sys
sys.path.append('/opt/sites/aws')
#path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#sys.path.append(path)
print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "okta.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
When running python to see what errors it gives I get
ModuleNotFoundError: No module named 'okta.settings'
I get the same error when running apache with debug logging.
I have quadruple checked the path and the environment variables and they are set right in the wsgi.py script.
Asked for the INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'okta_oauth2.apps.OktaOauth2Config',
'ops.apps.OpsConfig'
]
aws/
bin/
lib/
lib64/
logs/
okta/
__init__.py
urls.py
settings.py
wsgi.py
okta_oauth2/
ops/
share/
static/
manage.py
pyvenv.cfg

Although I did not figure out what was wrong. This is what I did to resolve.
Note for anyone reading this in the future, please DO NOT do this first. I tried other more common results like ImportError: No module named mysite.settings (Django) to fix the issue first and when none worked I tried this.
cd /opt/sites #Just outside the venv
sudo service httpd stop # stop web server
mv aws aws_broken_20201223 # move your broken env to a new name but do not delete
python3 -m venv aws #create new venv
cp -R aws_broken_20201223/(django dirs) aws/ #copy all the custom apps and project you have to the new folder
cd aws #go to the new install
bin/activate #activate your env
pip 3 install -r requirements.txt # I had a predefined requirements.txt for all mods I needed. I would suggest you do the same before doing this.
python3 okta/wsgi.py # Test to see if you are still getting the not found error
sudo service httpd start # start web server back up
voila it works without issue, no config change at all. Very strange but I'll take a working production site then a broken one.

Related

unable to install django restframework.auth_token

Trying to install from source
git clone https://github.com/tomchristie/django-rest-framework.git
cd django-rest-framework
setup.py install
settings.py
INSTALLED_APPS = (
...
'rest_framework.authtoken',
'rest_framework',
...
)
Trying to migrate
manage.py migrate
WindowsError: [Error 3] The system cannot find the path specified:
'C:\\Python27\\lib\\site-packages\\djangorestframework-3.0.0-py2.7.egg\\rest_framework\\authtoken\\migrations/*.*'
Whats the problem here ? Why is this a windows error ?
(rest_framework is migrating just fine ... )
Thanks.
I ended up copying the actual folder i cloned into c:\Python27\lin\sitepackages and renamed it to ...*.egg. It worked.

django.core.exceptions.ImproperlyConfigured:constance.backends.database app isn't installed correctly

Hi I am setting up my django with virtualenv
When I run python manage.py migrate, I got this error.
File "/Users/anh/testRadicards/env/lib/python2.7/site- packages/constance/backends/database/init.py", line 27, in init
"The constance.backends.database app isn't installed "
django.core.exceptions.ImproperlyConfigured: The constance.backends.database app isn't installed correctly. Make sure it's in your INSTALLED_APPS setting.
This is my setup, running with Mac OSX 10.5, on Python 2.7:
1. cd into my project dir
2. run virtualenv --no-site-packages env
3. activate my virtualenv with source/env/bin/activate
4. pip install django (currently 1.7.1)
5. git clone a django project repo called radicards
6. cd into radicards
7. run python.manage.py runserver
(The repo radicards already have a sqlite db setup, so there's no need for migration).
I followed the error code and open settings.py and it include this:
INSTALLED_APPS = (
'constance',
'constance.backends.database',
'rd_suit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'paintstore',
'cards',
)
'Contance.backends.database' is included. I heard django 1.7 has some issues with unique name error.
What should I do? To set this up.
Try adding this setting to your settings.py file:
CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'

Django, can't create app in subfolder

I'm on windows and trying to create a new app inside a common folder named Apps.
The myApp folder already exists inside Apps. I'm running from the project root:
python manage.py startapp myApp Apps\myApp
and I get:
Error: 'Apps\\myApp' is not a valid app name. Please use only numbers, letters and underscores.
I don't know why that double backslash.
I tried also with a forward slash just to be sure:
python manage.py startapp myApp Apps/myApp
and I get this:
Error: 'myApp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I can't understand if it is a Windows or a Python problem.
Try this:
mkdir Apps\newapp
python manage.py startapp NewApp Apps/newapp
And you will to create a app called "NewApp" inside folder "Apps/newapp".
Create your Apps directory from your project's root directory-
mkdir Apps
Move to your Apps directory-
cd Apps
Run python by calling the manage.py in your root project directory-
python ../manage.py startapp newapp
There you go
from the docs:
If the optional destination is provided, Django will use that existing directory rather than creating a new one. You can use '.' to denote the current working directory.
django-admin.py startapp myapp /Users/jezdez/Code/myapp
So try python manage.py startapp myApp ./Apps/myApp or with the full path.
Manage.py file is a thin wrapper of django-admin.py
In case, you want to create a new app in any directory
Try this:
$ cd <directory path>
$ django-admin.py startapp <app-anme>
For django 3.2.9
Create a sub directory Apps to hold all the apps, move into it, create a directory for the app myApp, then come back to root directory
mkdir Apps && cd Apps && mkdir myApp && cd ..
add a __init__.py file (is a python way to treat a directory as a package)
Create a myApp inside Apps sub directory
manage.py startapp myApp Apps/myApp
(in windows, please use backslash)
above two lines of code will create myApp
Now, in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local
'Apps.myApp.apps.MyAppConfig', # new
]
in ./Apps/myApp/apps.py file, change name='myApp' to name='Apps.myApp' will solve the issue
I had the same trouble on my Mac as well. I did solve it upgrading Django from vervion 1.3 to version 1.4.
As the documentation says you can use the command
django-admin startapp name [directory]
but in the example
django-admin startapp myapp /Users/jezdez/myapp
the documentation does not say that python creates the myapp folder. You should do it before the startapp command.

Django WSGI and Gunicorn

This might be really stupid question; I'm trying to deploy Django application using Gunicorn. However, I just created wsgi.py which looks like below (wsgi.py is in my root project folder):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
and now I ran:
python manage.py run_gunicorn
Will Gunicorn automatically picks up this wsgi.py? How does this work? (not sure what wsgi is doing). Or do i need to specify something?
If you have gunicorn listed in INSTALLED_APPS of Django settings module, the command is:
python manage.py run_gunicorn
Not the command you give.
I have Gunicorn to host my django site and this is the config details hope this is helpfull

Django not finding apps in virtualenv when using manage.py syncdb

My problem is in getting manage.py syncdb to run within a virtualenv.
It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.
Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.
When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.
Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,
For development, to create self-contained environments
For deployment, to create self-contained environments
In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.
Modified manage.py
#!/usr/bin/env python
import os.path
# Cater for Virtual env, add to sys.path
pwd = os.path.abspath(os.path.dirname(__file__))
project = os.path.basename(pwd)
new_path = pwd.strip(project)
activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:
TopLevelDir
|
|- Project DIR
|- venv
|- requirements
|- deployment configs
I have a simple solution to this
Just launch manage.py from the python in the bin of your virtual environment.
So say your python is here /home/tom/environments/my_env/bin/python you could launch manage.py like so:
/home/tom/environments/my_env/bin/python manage.py syncdb
then just create a symlink to the virtual environment's python inside your django project and call it env_python then you can do this:
./env_python manage.py syncdb