Django: manage.py works fine but django-admin fails - django

I've been starting up a new django project (django 1.8.13, python 3.4) and everything is working fine. However, I just noticed, django-admin returns these errors if I use it instead of manage.py
It's reporting there is no module named mysite (name of working project). It's no big deal as the site is fine but I was just wondering why this would happen, it's a brand new install and everything is fine except for this as far as I can tell.
(virtualenv) VirtualBox:# django-admin makemigrations
Traceback (most recent call last):
File "/usr/share/django-apps/virtualenv/bin/django-admin", line 11, in <module>
sys.exit(execute_from_command_line())
File "/usr/share/django-apps/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/usr/share/django-apps/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/usr/share/django-apps/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/share/django-apps/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/usr/share/django-apps/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/share/django-apps/virtualenv/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'mysite'

You are most likely seeing this error because some environment variables are not set, or set incorrectly. I would check to make sure that your PYTHONPATH, and DJANGO_SETTINGS_MODULE are all pointing to the correct locations.
PYTHONPATH is used to specify additional directories for Python to look for things to use. I think you'll want to set it to the directory which contains your manage.py script.
However, you can do everything that you need to do with manage.py

Related

Django KeyError when starting

I accidentally pulled an old git repo of my Django build ( It's running on a Heroku dyno ) and it had overwritten my entire local build, I managed to restore it back to the latest git repo which was previously working on my local build ( but not my Heroku dyno ) and the server now won't run locally or on my live build?
Error from the shell:
python manage.py runserver
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/johnnie/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/johnnie/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/home/johnnie/.local/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/johnnie/.local/lib/python3.7/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/johnnie/.local/lib/python3.7/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/johnnie/myproject/myproject/mysite/mysite/settings.py", line 141, in <module>
del DATABASES['default']['OPTIONS']['sslmode']
KeyError: 'OPTIONS'
edit:
del DATABASES['default']['OPTIONS']['sslmode']
was a section added by Heroku when committed by git.
removed this plus other Heroku changes which resolved issues.

Django, ModuleNotFoundError: No module named 'django_pg'

Can someone provide more details on setting up and utilizing django_pg. I would like to use the array functionality.
By following the setup of two simple steps from here:
https://django-pgfields.readthedocs.io/en/latest/usage.html
Results in this error:
(environment) jeff#jeff-computer:~/environment/projectA$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/jeff/environment/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/jeff/environment/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/jeff/environment/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/jeff/environment/lib/python3.7/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/home/jeff/environment/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/home/jeff/miniconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django_pg'
It looks like you forgot to do pip install django-pgfields to install it.
However, you probably shouldn’t be using this package. It hasn’t had any updates for years, so won’t support modern versions of Django.
Since Django 1.8,
Django comes with django.contrib.postgres, which might provide the Postgres functionality that you require.

No module named 'dj_database_url' while deploying Django app on Heroku

I have a problem while deploying my Django app on Heroku after the command :
heroku run python manage.py migrate
I get the following error text :
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 308, in execute
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/foodtasker/settings.py", line 145, in <module>
import dj_database_url
ModuleNotFoundError: No module named 'dj_database_url'
When I run pip freeze it appears that dj_database_url already exists.
My pip freeze gives :
authy==2.2.3
certifi==2018.11.29
chardet==3.0.4
defusedxml==0.5.0
dj-database-url==0.5.0
Django==1.10
django-bootstrap3==7.0.1
django-braces==1.13.0
django-extensions==2.1.4
django-oauth-toolkit==1.0.0
django-oauth2-provider==0.2.6.1
django-rest-framework-social-oauth2==1.0.7
djangorestframework==3.9.0
gunicorn==19.6.0
idna==2.8
oauthlib==2.1.0
phonenumbers==8.10.2
Pillow==3.3.0
psycopg2==2.7.4
PyJWT==1.7.1
python-dotenv==0.10.1
python-social==0.1
python-social-auth==0.3.6
python3-openid==3.1.0
pytz==2018.7
requests==2.21.0
requests-oauthlib==1.0.0
shortuuid==0.5.0
simplejson==3.16.0
six==1.12.0
social-auth-app-django==3.1.0
social-auth-core==2.0.0
urllib3==1.24.1
whitenoise==3.2.1
For the current state I don't figure out the source of the error.
I am at a loss and I hope someone might be able to help with this as well as hoping

django-admin "ModuleNotFoundError:" after change in settings structure for development & production

Background
I have recently started to learn Python Django. I read that it was good practice to have separate settings file for different environments. Consequently I have tried to implement something similar to what is describe in the "Simple Package Organization for Environments" section of this wiki: https://code.djangoproject.com/wiki/SplitSettings
Problem
When I now run a django-admin command I get a ModuleNotFoundError. Below I have copy pasted the error log I get for "django-admin check --deploy". "python manage.py runserver --settings=CollegeComp.settings.development" works fine.
Things I've tried
I was reading that I may have to reset the DJANGO_SETTINGS_MODULE environment variable in my virtual environment. I entered "set DJANGO_SETTINGS_MODULE=CollegeComp.settings.development" but I still get the same error.
Python path
When I type the following in the shell with my virtual environment activated:
import sys
print(sys.path)
I get the following:
['C:\\Users\\myusername\\Documents\\UdemyDjango\\MyPersonalProject\\College-Project-master\\CollegeComp',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\python37.zip',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\DLLs',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\lib',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\lib\\site-packages']
Error log
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\Scripts\django-admin-script.py", line 10, in <module>
sys.exit(execute_from_command_line())
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 329, in run_from_argv
connections.close_all()
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 220, in close_all
for alias in self:
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 214, in __iter__
return iter(self.databases)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 147, in databases
self._databases = settings.DATABASES
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
self._setup(name)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'CollegeComp'
My project directory
CollegeComp
|-CollegeComp
|-settings
|-__init__.py
|-base.py
|-development.py
|-local.py
|-production.py
|-__init__.py
|-urls.py
|-wsgi.py
|-<my apps>
base.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
<rest of standard Django settings.py code>
development.py
from CollegeComp.settings.base import *
#Override base.py settings here
DEBUG = True
#Import local setting file
try:
from CollegeComp.settings.local import *
except:
pass
local.py
from CollegeComp.settings.base import *
#Override base settings here
DEBUG = True
production.py
from CollegeComp.settings.base import *
#Override base settings here
DEBUG = False
#Import local setting file
try:
from CollegeComp.settings.local import *
except:
pass
System info
Django version 2.1
Operating system: Windows 10
Python version: 3.7.0
I am grateful for any help I can get.

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