I'm using Django and Python 3.7. I have different database configurations per environment, so I wanted to create settings that only import the database config depending on the environment. I have this directory ...
mainpage_project/settings
and within it are these files. "base.py" contains settings that I wish to be included regardless of the environment taht is loaded ...
(venv) localhost:mainpage_project davea$ ls mainpage_project/settings
__pycache__ base.py dev.py prod.py test.py
I have this in my mainpage_project/settings/test.py file ...
from mainpage.base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'testdatabase'
},
}
But when I go to run my test file, I get the error complaining that it cannot execute the first line of the import, "from mainpage.base import *" ...
(venv) localhost:mainpage_project davea$ cd /Users/davea/Documents/workspace/mainpage_project; source ./venv/bin/activate; python manage.py test
Traceback (most recent call last):
File "manage.py", line 21, in <module>
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 308, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 282, in create_parser
self.add_arguments(parser)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/core/management/commands/test.py", line 47, in add_arguments
test_runner_class = get_runner(settings, self.test_runner)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/test/utils.py", line 301, in get_runner
test_runner_class = test_runner_class or settings.TEST_RUNNER
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Library/Frameworks/Python.framework/Versions/3.7/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 "/Users/davea/Documents/workspace/mainpage_project/mainpage_project/settings/test.py", line 1, in <module>
from mainpage.base import *
ModuleNotFoundError: No module named 'mainpage.base'
How do I properly reference my settings?
I'm not sure where you're getting the mainpage part of from mainpage.base import *.
You can either change it to the correct absolute import:
from mainpage_project.settings.base import *
Or relative import:
from .base import *
Note that the absolute import requires /Users/davea/Documents/workspac to be in PATH.
Related
(Migrate may not be the correct term I'm unsure) I'm trying to figure out how to change my Django site to register SQLite. Originally SQLite is the default for Django however I changed to MySQL because the original site I was going to host on (GoDaddy) was compatible with it. The only solution I thought of so far is changing the DATABASES info in my Settings.py. After changing my Settings to the defaults of
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
I ran into this error,
(env) PS C:\Users\kayak\New Work\google athentication\mysite> python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\kayak\New Work\google athentication\mysite\manage.py", line 22, in <module>
main()
File "C:\Users\kayak\New Work\google athentication\mysite\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\django\core\management\__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\django\conf\__init__.py", line 92, in __getattr__
self._setup(name)
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\django\conf\__init__.py", line 79, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\django\conf\__init__.py", line 190, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\kayak\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\kayak\New Work\google athentication\mysite\mysite\settings.py", line 12, in <module>
import heroku
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\heroku\__init__.py", line 27, in <module>
from .core import from_key, get_key, from_pass
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\heroku\core.py", line 10, in <module>
from .api import Heroku
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\heroku\api.py", line 11, in <module>
from .helpers import is_collection
File "C:\Users\kayak\New Work\google athentication\env\lib\site-packages\heroku\helpers.py", line 57
except TypeError, e:
^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
Thanks for any incite and or help in advance!
structure = mysite
mysite
settings - base.py, deployment.py, production.py
init.py
asgi.py
urls.py
wsgi.py
base.py has all the base settings.
deployment.py has code
from .base import *
DEBUG = True
SECRET_KEY = os.environ['SECRET_KEY']
after running py manage.py runserver --settings=mysite.settings.development
i got the error as follows :
(venv) C:\Users\Pc\PycharmProjects\mysite>py manage.py runserver --settings=mysite.settings.development
import export
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Pc\PycharmProjects\mysite\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Pc\PycharmProjects\mysite\venv\lib\site-packages\django\core\management\__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "C:\Users\Pc\PycharmProjects\mysite\venv\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "C:\Users\Pc\PycharmProjects\mysite\venv\lib\site-packages\django\conf\__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\Pc\PycharmProjects\mysite\venv\lib\site-packages\django\conf\__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\Pc\AppData\Local\Programs\Python\Python38\lib\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 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Rohan\PycharmProjects\mysite\mysite\settings\development.py", line 5, in <module>
SECRET_KEY = os.environ['SECRET_KEY']
File "C:\Users\Rohan\AppData\Local\Programs\Python\Python38\lib\os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
any suggestions
That suggests you didn't add SECRET_KEY into your os environment.
You could do the following:
Add the secret key into your os (seems you are using Windows): setx SECRET_KEY <your secret key> in command promt.
In your settings.py, add this SECRET_KEY = os.getenv("SECRET_KEY")
I'm tryin to deploy to Heroku and before this error I was getting:
ModuleNotFoundError: No module named 'django-tables2'
Then I installed django-heroku via pip install django-heroku, followed the instructions on how to set it up.
I disabled the collect static for heroku pushed my master branch and everything is fine, but once I do:
heroku run python manage.py migrate
I get the:
ModuleNotFoundError: No module named 'django_heroku'
the complete traceback is this:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 341, in run_from_argv
connections.close_all()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 225, in close_all
for alias in self:
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 219, in __iter__
return iter(self.databases)
File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 153, in databases
self._databases = settings.DATABASES
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 142, 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/GPC/settings.py", line 14, in <module>
import django_heroku
ModuleNotFoundError: No module named 'django_heroku'
I have in my settings at the top:
import os
import django_heroku
and in the very bottom:
django_heroku.settings(locals())
in my requirements.txt I have django-heroku
The master branch is updated and I just can find how to fix this.
The problem was that it was not added to the module, it was needed to be added to the virtual env and then do the pip freeze, with that it was solved.
My problem is this: When I try to do the migrations, it gives me the following error:
python3 manage.py makemigrations
Traceback (most recent call last):
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/jenifer/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/jenifer/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/jenifer/.local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/jenifer/.local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/jenifer/.local/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
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/jenifer/.local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/jenifer/.local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/models/base.py", line 121, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/models/options.py", line 208, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/utils.py", line 207, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/jenifer/.local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 21, in <module>
) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
Thing is, I've never told django to use MySQL, in fact, the settings.py states that it has to use SQLite:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'gestlote.db'),
}
}
I've alreay tried to reinstall django, upgrade it, pip3 install pysqlite, but nothing seems to work.
So please, help me
Hi I just started my Django project today and during my attempt to python manage.py migrate I got the following error:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/danni/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/danni/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/danni/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 216, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/danni/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 36, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/danni/anaconda3/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 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 "/home/danni/.local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 12, in <module>
from django.db.migrations.autodetector import MigrationAutodetector
File "/home/danni/.local/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 11, in <module>
from django.db.migrations.questioner import MigrationQuestioner
File "/home/danni/.local/lib/python3.6/site-packages/django/db/migrations/questioner.py", line 9, in <module>
from .loader import MigrationLoader
File "/home/danni/.local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 8, in <module>
from django.db.migrations.recorder import MigrationRecorder
File "/home/danni/.local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 9, in <module>
class MigrationRecorder:
File "/home/danni/.local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 22, in MigrationRecorder
class Migration(models.Model):
File "/home/danni/.local/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/danni/.local/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "/home/danni/.local/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Then I ran python manage.py check and got following error:
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
So far my project structure is:
I created a settings folder at project/project/settings and have three file base.py(The one I renamed and moved from project/project/name), production.py, staging.py, test.py which only includes one line from base import * and also an empty __init__.py.
For the account app it's under apps/account. And I already added urls.py. Also, I have added 'account' under INSTALLED_APPS in base.py.
Can someone please tell me how to fix this? Thanks a lot!
As you have changed the settings.py file to settings folder, you have to update the manage.py file to point out to the new settings module, In manage.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
# Here update the string 'example.settings' to the new settings module
# e.g os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings.test')