DJANGO_SETTINGS_MODULE No Module Named - django

I am trying to create a population script for my database, but I get a No module named error.
Project Structure:
staticone/
manage.py
mysite/
populate_rango
settings
urls
__init__
wsgi
rango/
Traceback:
Traceback (most recent call last):
File "populate_rango.py", line 8, in <module>
django.setup()
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/ubuntu/virtenv/webstatic/lib/python3.5/site-
packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'mysite'
My path:
['/home/ubuntu/virtenv/webstatic/staticone/mysite',
'/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-
x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload',
'/home/ubuntu/virtenv/webstatic/lib/python3.5/site-packages']
My settings.py file is under mysite
populate_rango.py under mysite folder:
import sys
print (sys.path)
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','settings')
import django
django.setup()
from mysite.rango.models import Category,Page
If I try
os.environ.setdefault('DJANGO_SETTINGS_MODULE','mysite.settings')
I get the same error:
No module named mysite.
mysite is on the path, so I am confused as to why it is not finding the settings file.

I needed to put the parent directory of mysite on the path. I only had /home/ubuntu/virtenv/webstatic/staticone/mysite. Now it works when I included
/home/ubuntu/virtenv/webstatic/staticone/.
import sys
sys.path.append("/home/ubuntu/virtenv/webstatic/staticone/")
print (sys.path)
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','mysite.settings')
import django
django.setup()
from mysite.rango.models import Category,Page
Now it works.

Related

How to access a Django model outside a Django project?

The problem below is raised after reviewing all the related SO questions, but unfortunately none of the answers brought a solution in my case (similar problem like: How to access Django models outside of Django?).
(with Django version: 4.1.1)
Having an application "simo" available with the related model, the db access works well from Django shell; but trying to access to it from outside Django as "scheduling.py", it just does not work.
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
django.setup()
from django.db import models
from simo.models import Tasks
I tried to execute my python script in any folder:
beside manage.py,
beside settings.py and
beside models.py but none of them works.
[mysite]
[mysite]
[simo]
[migrations]
views.py
models.py
urls.py
__init__.py
apps.py
tests.py
admin.py
settings.py
urls.py
__init__.py
wsgi.py
asgi.py
scheduling.py
__init__.py
db.sqlite3
manage.py
The error received is:
C:\...\python.exe C:/.../mysite/scheduling.py
Traceback (most recent call last):
File "C:\...\mysite\scheduling.py", line 5, in <module>
django.setup()
File "C:\...\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\...\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\...\lib\site-packages\django\apps\config.py", line 178, in create
mod = import_module(mod_path)
File "C:\...\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'simo'
Process finished with exit code 1
Could anyone advise me on how to resolve this issue?
Try calling
from django.db import models and from simo.models import Tasks before django.setup()

Django ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'

I know others have had a similar issues with and getting this same error, but I think my situation is unique.
I am running Django 3.1.4 and on my local machine, I can run python manage.py shell with no issue.
On the server instance, running what should be the same project, and the same version of Django, I get:
Django ImportError: cannot import name 'python_2_unicode_compatible'
from 'django.utils.encoding'
When trying to run manage.py shell. To make things more cryptic, if I open the shell on my local machine and run:
from django.utils.encoding import python_2_unicode_compatible
I get the same error. So for some reason when I call manage.py shell from my local machine it doesn't try to import python_2_unicode_compatible, but when I run it from the server it does. I can't find where the discrepancy is.
Here is the full stacktrace if that is helpful:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/chase/Env/mantis/lib/python3.8/site-packages/request/models.py", line 7, in <module>
from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (/home/chase/Env/mantis/lib/python3.8/site-packages/django/utils/encoding.py)
Any ideas on where to start poking around?
You can try this.
First, install six:
pip install six
Then go to the django.utils.encoding.py file and simply import python_2_unicode_compatible from six like this:
from six import python_2_unicode_compatible

Web application could not be started by the Phusion Passenger application server, ModuleNotFoundError - Django [SOLVED]

when I install a third-site application from github with pip install -e git+https://github.com/breduin/das.git#egg=django_ajax_selects my site doesn't start and the following error raises:
Web application could not be started by the Phusion Passenger application server.
/usr/share/passenger/helper-scripts/wsgi-loader.py:26: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import sys, os, re, imp, threading, signal, traceback, socket, select, struct, logging, errno
Traceback (most recent call last):
File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 369, in <module>
app_module = load_app()
File "/usr/share/passenger/helper-scripts/wsgi-loader.py", line 76, in load_app
return imp.load_source('passenger_wsgi', startup_file)
File "/opt/python/python-3.8.6/lib/python3.8/imp.py", line 171, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/www/u1234567/data/www/mysite/passenger_wsgi.py", line 7, in <module>
application = get_wsgi_application()
File "/var/www/u1234567/data/env/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/var/www/u1234567/data/env/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/var/www/u1234567/data/env/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/var/www/u1234567/data/env/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/opt/python/python-3.8.6/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'ajax_select'
In the directory env/lib/python3.8/site-packages/, where usually all the packages from PyPI are placed one can find the egg-file,
django-ajax-selects.egg-link
/var/www/u1234567/data/env/src/django-ajax-selects
.
The path is correct, the application django-ajax-selects is placed there.
Without this application from github and with other applications from PyPI my site starts ok.
It seems like server doesn't recognize egg-link or path, but I couldn't find out how to correct this.
SO-effect, while writing the question I found the answer by myself: one needs to add the path to the package (which is in the egg-link file) in passenger_wsgi.py:
# -*- coding: utf-8 -*-
import os, sys
sys.path.insert(0, '/var/www/u1234567/data/www/mysite/mysite')
sys.path.insert(1, '/var/www/u1234567/data/env/lib/python3.8/site-packages')
sys.path.insert(2, '/var/www/u1234567/data/env/src/django-ajax-selects') #<---- this path
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Cannot fix django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

I run the following line of code on a Docker container:
RUN python3 manage.py sitetree_resync_apps --settings=sites.production.settings
and I get the following error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/xxx/xxx/invoicing/apps.py", line 10, in <module>
from xxx.site.signals import resource_event, notify_by_email
File "/xxx/xxx/site/signals.py", line 12, in <module>
from xxx.backend.email import send_email
File "/xxx/xxx/backend/email.py", line 11, in <module>
from post_office import mail
File "/usr/local/lib/python3.6/site-packages/post_office/mail.py", line 13, in <module>
from .models import Email, EmailTemplate, Log, PRIORITY, STATUS
File "/usr/local/lib/python3.6/site-packages/post_office/models.py", line 27, in <module>
class Email(models.Model):
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I have tried to update Django version and several other libraries. The error changes but once fixed in the end I always end up on the above error! Can someone help?
my requirement file looks like:
Django==3.1.3
Pillow==3.0.0
XlsxWriter==0.8.4
amqp==1.4.9
anyjson==0.3.3
backports.functools-lru-cache==1.6.1
beautifulsoup4==4.8.2
billiard==3.3.0.23
celery==3.1.26.post2
cssutils==1.0.2
django-autocomplete-light==2.1.1
django-braces==1.14.0
django-bootstrap3==6.2.2
django-bootstrap3-datetimepicker-2==2.4.2
django-celery==3.3.1
django-cors-headers==3.5.0
django-crispy-forms==1.5.2
django-datatable-view==0.8.2
django-extensions==2.0.7
django-formtools==1.0
django-grappelli==2.7.1
django-ical==1.3
django-hstore==1.4.2
django-inlinecss==0.1.2
django-model-utils==2.5.2
django-money==0.7.4
django-oauth-toolkit==1.0.0
djangorestframework==3.9.4
django-sortedm2m==2.0.0
django-sitetree==1.16.0
django-tag-parser==2.1
django-taggit==0.17.1
django-tastypie==0.12.2
django-timedeltafield==0.7.8
django-post-office==3.5.2
et-xmlfile==1.0.1
funcsigs==1.0.2
icalendar==4.0.4
jdcal==1.4.1
jsonfield==3.1.0
jsonpickle==1.4.1
kombu==3.0.37
matplotlib==3.2.2
mock==3.0.5
numpy==1.16.5
oauthlib==2.0.1
openpyxl==2.3.0
pandas==0.24.2
psycopg2==2.6
py-moneyed==0.6.0
pynliner3==0.6
python-dateutil==2.8.1
python-mimeparse==1.6.0
pytz==2019.3
six==1.10.0
soupsieve==1.9.5
uWSGI==2.0.18
unicodecsv==0.14.1
vine==1.3.0
I located the error on the manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
If I comment out the offending line in the Dockerfile the container successfully build but when I try to hop into it with the code:
docker-compose up -d && docker-compose logs -f
I get the error:
nettle_1 | Traceback (most recent call last):
nettle_1 | File "manage.py", line 8, in <module>
nettle_1 | from django.core.management import execute_from_command_line
nettle_1 | ImportError: No module named django.core.management
exited with code 1
I am completely lost here.
EDITED
Hi Melvin,
this is my app.py
from mcvitty.site.signals import resource_event, notify_by_email
from django.apps import AppConfig
class InvoicingConfig(AppConfig):
name = 'mcvitty.invoicing'
verbose_name = "Invoices"
def ready(self):
resource_event.connect(notify_by_email, sender='invoicing.Invoice')
Due to my lack of experience with django I still fails to understand the error and how to set app.py correctly
Your problem is here:
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/xxx/xxx/invoicing/apps.py", line 10, in <module>
from xxx.site.signals import resource_event, notify_by_email
File "/xxx/xxx/site/signals.py", line 12, in <module>
from xxx.backend.email import send_email
File "/xxx/xxx/backend/email.py", line 11, in <module>
from post_office import mail
Your invoicing apps imports signals that import models from post_office. You should only register signals when all apps are loaded, through AppConfig.ready() as is explained in the documentation:
Subclasses can override this method to perform initialization tasks such as registering signals. It is called as soon as the registry is fully populated.
For clarity: this includes importing utilities / modules that import models from other apps as that lies at the core of the problem.

Django: Changing project folder / settings structure

I am currently trying to change from the default Django structure to that one.
I now copied all the files in the new folders, but when running python manage.py runserver --settings=settings.local it shows the following in my terminal:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'settings'
Can anyone see what I am doing wrong?
I also changed in my wsgi.py and manage.py to the following:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
You need to turn the config/ directory into a python module by creating a blank config/__init__.py file, then configure your DJANGO_SETTINGS_MODULE like this in manage.py and config/wsgi.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
Then you should be able to just run ./manage.py runserver without the --settings option.
You need to tell django where to look for settings module therefore you have to specify the module path in manage.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
and in __init__.py inside settings folder add the following lines:
try:
from config.settings.local import *
except:
pass
if you are using Pycharm go to
File > Settings > Language and Frameworks >Django
and configure that
1)[checked] Django support
2) settings: config\settings.py
3)manage script: manage.py
But if you are using a independent console it should be works fine just with the ___init__.py file in the folder config ,and the configuration os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") in the manage.py file,using
python manage.py runserver