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")
Related
I am getting the following error in when I run the server after I changed BASE_DIR:
$ python manage.py run
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\NAZIA\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\NAZIA\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "C:\Users\NAZIA\AppData\Local\Programs\Python\Python38\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__
self._setup(name)
File "C:\Users\NAZIA\AppData\Local\Programs\Python\Python38\lib\site-packages\django\conf\__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\NAZIA\AppData\Local\Programs\Python\Python38\lib\site-packages\django\conf\__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\NAZIA\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:\website\website\website\settings.py", line 72, in <module>
'NAME': BASE_DIR / 'db.sqlite3',
TypeError: unsupported operand type(s) for /: 'str' and 'str'
You can try something like this:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MEDIA_PATH = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/social_website/uploads/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, *MEDIA_URL.strip("/").split("/"))
In mysite/settings.py I have:
SECRET_KEY = os.environ["SECRET_KEY"]
Using the GUI Control Panel ---> System Properties ---> Advanced ---> Environmental Variables, I have set the user environmental variables:
Variable Value
SECRET_KEY 123456
But am still getting the key is empty error when I run manage.py
Failed to get real commands on module "mysite": python process died with code 1: Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module>
django.setup()
File "C:\Users\super\Desktop\GitHub\Django_Tutorial_Site\mysite\venv\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\super\Desktop\GitHub\Django_Tutorial_Site\mysite\venv\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "C:\Users\super\Desktop\GitHub\Django_Tutorial_Site\mysite\venv\lib\site-packages\django\conf\__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\super\Desktop\GitHub\Django_Tutorial_Site\mysite\venv\lib\site-packages\django\conf\__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\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\super\Desktop\GitHub\Django_Tutorial_Site\mysite\mysite\settings.py", line 11, in <module>
SECRET_KEY = os.environ["SECRET_KEY"]
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: '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.
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.
I have been following tutorial how to import celery and run rabbitmq to my django project. and I have been following a tutorial on youtube. I do not now what I have missed but after the part where i ran python manage.py migrate djcelery it gives the result of:
root#1095ea233e7b:/app/main# python3 manage.py migrate djcelery
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 302, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 99, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/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 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/app/main/actinbox/settings.py", line 6, in <module>
import djcelery
this is the tutorial that I have been following:
Django Celery Part 1
As I said in a comment, it's probably because you installed celery 4.0.0.
the tutorial you followed uses celery 3.0.23.
If you really want to use the tutorial use pip install django-celery==3.0.23
this is not recommended because it's an outdated version.