Sqlalchemy ValueError: invalid literal for int() with base 10: 'None' - python

I'm trying to run the following code but I got a sqlalchemy invalid literal for int() with base 10: 'None' error.
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
load_dotenv('.env')
#MOben DB
moben_db_creds = {
'host' : os.getenv('POSTGRES_MOBEN_HOST'),
'port' : os.getenv('POSTGRES_MOBEN_PORT'),
'user' : os.getenv('POSTGRES_MOBEN_USER'),
'password' : os.getenv('POSTGRES_MOBEN_DBPASS'),
'name' : os.getenv('POSTGRES_MOBEN_DBNAME'),
'type' : os.getenv('POSTGRES_MOBEN_DBTYPE')
}
sqlalchemy_db_uri_conn = f"{moben_db_creds['type']}://{moben_db_creds['user']}:{moben_db_creds['password']}#{moben_db_creds['host']}:{moben_db_creds['port']}/{moben_db_creds['name']}"
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = create_engine(sqlalchemy_db_uri_conn)
# CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE
session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
base = declarative_base()
def get_db():
db = session()
try:
yield db
finally:
db.close()
As i am quite new to this orm, unable to identify the solution to this error.
please follow below Traceback for more detail
Traceback: ==============================================================
(env_ucom_csb) PS C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher> uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['C:\\Users\\e008329\\Downloads\\project_zone\\ucom-csb-payload-publisher']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [27160] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\server.py", line 67, in serve
config.load()
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\config.py", line 477, in load
self.loaded_app = import_from_string(self.app)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "C:\Users\e008329\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 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\main.py", line 3, in <module>
from publish_ucom_csb import publish_uci_all
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\publish_ucom_csb.py", line 2, in <module>
from models.v_payload_ucom_csb import VPayloadUCOMCsb
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\models\v_payload_ucom_csb.py", line 2, in <module> from config.database import base
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\config\database.py", line 31, in <module>
engine = create_engine(sqlalchemy_db_uri_conn)
File "<string>", line 2, in create_engine
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\util\deprecations.py", line 277, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\create.py", line 552, in create_engine
u = _url.make_url(url)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\url.py", line 838, in make_url return _parse_url(name_or_url)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\url.py", line 893, in _parse_url
components["port"] = int(components["port"])
ValueError: invalid literal for int() with base 10: 'None'

You can pass a default port value when you are using the os.getenv (see https://docs.python.org/3/library/os.html#os.getenv)
e.g.
DEFAULT_PORT_VALUE = 9999
moben_db_creds = {
'host' : os.getenv('POSTGRES_MOBEN_HOST'),
'port' : os.getenv('POSTGRES_MOBEN_PORT', DEFAULT_PORT_VALUE),
...
}
Right now it seems you are passing None in the line referenced by the traceback:
components["port"] = int(components["port"])
The other option would be to use get instead of [] syntax to get the value from the components dict.
components["port"] = int(components.get("port", DEFAULT_PORT_VALUE))
Btw. it's necessarily about the env file contents it might be about its location (or the command you are using to run the script / start the server). Print or log the values after you get them from the env file and see if they are being read correctly.

Related

when i runserver (python manage.py runserver) this error is raised: TypeError: a bytes-like object is required, not 'str'

I tried deploying my django/React (using postgreSQL) app using Render. The deploy worked but no data is being returned.
Also, when I try to load seed data or run the server from my directory the following error is thrown:
Traceback (most recent call last):
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 22, in <module>
main()
File "/Users/xxx/xxx/xxx/xxx/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/core/management/__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 87, in __getattr__
self._setup(name)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/django/conf/__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python#3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/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 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/xxx/xxx/xxx/xxx/project/settings.py", line 114, in <module>
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
File "/Users/xxx/.local/share/virtualenvs/xxx/lib/python3.9/site-packages/dj_database_url.py", line 94, in parse
if "?" in path and not url.query:
TypeError: a bytes-like object is required, not 'str'
Here's part of my settings.py:
DATABASES = {
'default': dj_database_url.parse(os.environ.get('DATABASE_URL'), conn_max_age=600),
}
I've also tried:
DATABASE_URL = os.environ.get('DATABASE_URL')
DATABASES = {
'default': dj_database_url.config()
}
But when I run the server, the app loads but has no data (just like the deployed app).
The code block surrounding line 94 in dj_database_url.py is as follows:
# Split query strings from path.
path = url.path[1:]
if "?" in path and not url.query:
path, query = path.split("?", 2)
else:
path, query = path, url.query
query = urlparse.parse_qs(query)
type(os.environ.get('DATABASE_URL')) returns <class 'NoneType'>
and the value returned for os.environ.get('DATABASE_URL') is None

Unable to set cloud spanner dialect for Alembic

I have installed the following packages -
SQLAlchemy==1.4.41
sqlalchemy-spanner==1.2.2
alembic==1.8.1
Then I created a file main.py where I am creating a SQLAlchemy engine for cloud spanner.
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine(
"spanner+spanner:///projects/spanner-project/instances/spanner-instance/databases/spanner-database"
)
metadata = MetaData()
Base = declarative_base(metadata=metadata)
Then in alembic.ini I have used the same url as shown above for engine.
sqlalchemy.url = spanner+spanner:///projects/spanner-project/instances/spanner-instance/databases/spanner-database
Also in env.py I have set the base's matadata as shown below -
from app.main import Base
target_metadata = Base.metadata
I also have a models.py which has just one model as shown below -
from app.main import Base
from sqlalchemy import (
Column,
Integer,
String,
)
class Users(Base):
__tablename__ = "users"
__table_args__ = {
'extend_existing': True,
}
id = Column(Integer, primary_key=True)
name = Column(String(50))
When I run the alembic command to generate migrations I see an exception of KeyError: 'spanner+spanner'.
CMD -
alembic revision --autogenerate -m "first migrations"
Exception -
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 590, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 584, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 561, in run_cmd
fn(
File "/usr/local/lib/python3.10/site-packages/alembic/command.py", line 229, in revision
script_directory.run_env()
File "/usr/local/lib/python3.10/site-packages/alembic/script/base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/code/app/alembic/env.py", line 79, in <module>
run_migrations_online()
File "/code/app/alembic/env.py", line 68, in run_migrations_online
context.configure(
File "<string>", line 8, in configure
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/environment.py", line 822, in configure
self._migration_context = MigrationContext.configure(
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/migration.py", line 268, in configure
return MigrationContext(dialect, connection, opts, environment_context)
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/migration.py", line 196, in __init__
self.impl = ddl.DefaultImpl.get_by_dialect(dialect)(
File "/usr/local/lib/python3.10/site-packages/alembic/ddl/impl.py", line 123, in get_by_dialect
return _impls[dialect.name]
I tried to by changing the URL in alembic.ini from spanner+sapnner:///..... to spanner:///... but then I got this exception -
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 590, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 584, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 561, in run_cmd
fn(
File "/usr/local/lib/python3.10/site-packages/alembic/command.py", line 229, in revision
script_directory.run_env()
File "/usr/local/lib/python3.10/site-packages/alembic/script/base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/code/app/alembic/env.py", line 79, in <module>
run_migrations_online()
File "/code/app/alembic/env.py", line 61, in run_migrations_online
connectable = engine_from_config(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 743, in engine_from_config
return create_engine(url, **options)
File "<string>", line 2, in create_engine
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 522, in create_engine
entrypoint = u._get_entrypoint()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/url.py", line 655, in _get_entrypoint
cls = registry.load(name)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 343, in load
raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:spanner
Also when I use spanner+spanner:///... to creating and engine and then further use it to create db tables then I am able to do that successfully.
from app.main import engine, Base
from sqlalchemy import (
Column,
Integer,
String,
)
class Users(Base):
__tablename__ = "users"
__table_args__ = {
'extend_existing': True,
}
id = Column(Integer, primary_key=True)
name = Column(String(50))
Base.metadata.create_all(engine)
Just by running this model I am able to create the table.
python models.py
What do I do to use alembic with spanner?
You're using SQLAlchemy 1.4, which means it must be everywhere spanner+spanner. Just spanner will not work. As the problem appears only for Alembic, reinstall will also not help - the problem is in configurations somewhere.
Can you show your env.py file? There must be written something like this:
class SpannerImpl(DefaultImpl):
__dialect__ = "spanner+spanner"
Looking at your traceback, I suspect, in the env.py the dialect is written as just spanner, which causes the error.

Django-stubs doesn't ignore import

I've tried to configurate pre-commit + mypy + django-stubs, so I have error when i try to commit.
ModuleNotFoundError: No module named 'environ'
My configs:
.pre-commit-config.yaml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: ''
hooks:
- id: mypy
exclude: "[a-zA-Z]*/(migrations)/(.)*"
args: [--config=setup.cfg,
--no-strict-optional,
--ignore-missing-imports]
additional_dependencies: [django-stubs]
setup.cfg
[mypy]
allow_redefinition = True
check_untyped_defs = True
ignore_missing_imports = True
incremental = True
strict_optional = True
show_traceback = True
warn_no_return = False
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
plugins = mypy_django_plugin.main
show_column_numbers = True
[mypy.plugins.django-stubs]
django_settings_module = config.settings.local
[mypy_django_plugin]
ignore_missing_model_attributes = True
[mypy-*.migrations.*]
# Django migrations should not produce any errors:
ignore_errors = True
It's look like mypy ignore option ignore_missing_imports = True.
Does anyone have any ideas?
Full error trace:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/**/.cache/pre-commit/patch1646008720-58913.
[WARNING] The 'rev' field of repo 'https://github.com/pre-commit/mirrors-mypy' appears to be a mutable reference (moving tag / branch). Mutable references are never updated after first install and are not supported. See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details. Hint: `pre-commit autoupdate` often fixes this.
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...............................................................Passed
black....................................................................Passed
isort....................................................................Passed
dotenv-linter........................................(no files to check)Skipped
yamllint.................................................................Passed
flake8...................................................................Passed
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1
Error constructing plugin instance of NewSemanalDjangoPlugin
Traceback (most recent call last):
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy/__main__.py", line 12, in console_entry
main(None, sys.stdout, sys.stderr)
File "mypy/main.py", line 96, in main
File "mypy/main.py", line 173, in run_build
File "mypy/build.py", line 180, in build
File "mypy/build.py", line 231, in _build
File "mypy/build.py", line 478, in load_plugins
File "mypy/build.py", line 456, in load_plugins_from_config
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/main.py", line 143, in __init__
self.django_context = DjangoContext(django_settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 95, in __init__
apps, settings = initialize_django(self.django_settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 77, in initialize_django
settings._setup()
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 71, in _setup
self._wrapped = Settings(settings_module)
File "/home/**/.cache/pre-commit/repon7zquz0p/py_env-python3.9/lib/python3.9/site-packages/django/conf/__init__.py", line 179, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/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 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/**/project/config/settings/local.py", line 3, in <module>
from .base import * # noqa
File "/home/**/project/config/settings/base.py", line 6, in <module>
import environ
ModuleNotFoundError: No module named 'environ'
I have to write some more text here, because stackoverflow get error: a lot of code, add details.

How use django with decouple

I am trying to use python-decouple for sensitive data in my project but
When i use decouple.config for SECRET_KEY it raises an error
error
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/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/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/admin/Legaland/src/sqh/settings/dev.py", line 1, in <module>
from ._base import *
File "/home/admin/Legaland/src/sqh/settings/_base.py", line 40, in <module>
SECRET_KEY = config("SECRET_KEY")
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 65, in get
value = self.repository[option]
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 113, in __getitem__
return self.parser.get(self.SECTION, key)
File "/usr/lib/python3.6/configparser.py", line 800, in get
d)
File "/usr/lib/python3.6/configparser.py", line 394, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.6/configparser.py", line 444, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%^e=(r2l0*73)4zxv!(!4x(%(_koxr049zlesn3"'
How shall i make it read SECRET_KEY as text
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config("SECRET_KEY")
I am already I using .ini file for my email address and password the problem is that it tries to perform SECRET_KEY instead of accepting it as a raw text how can i prevent it
Right to the point, as said by Henrique Bastos, here:
Note: Since ConfigParser supports string interpolation, to represent the character % you need to escape it as %%.
To use python-decouple with a .ini file you can do this:
Use your variable like said in the first answer:
SECRET_KEY = config(SECRET_KEY, default='some-random-text')
You can use cast, if needed, like:
DEBUG = config(DEBUG, default=True, cast=bool)# in the .ini file, the DEBUG value can be: 0, 1, True, False...
So for last, your traceback error show that there is a percent sign in your .ini string value, you may need to do a scape with a %% as said in the official documentation https://github.com/henriquebastos/python-decouple
Do a escape parser with double percents, instead just one and the error will be gone!
you should have a env file like below
.env
SECRET_KEY='aaassskkkk'
and then source your .env file using below command
source .env
then try the below code it will works
from decouple import config
SECRET_KEY = config('SECRET_KEY', '')
if the SECRET_KEY not found in env it will take as ''
Decouple supports .ini and .env files. For use .env file simply create a .env text file in your repository's root directory.
SECRET_KEY='super-secret-key'
you can also override the parameter
SECRET_KEY='other-secret-key' python manage.py runserver

"Apps aren't loaded yet" when import model to a file run by manage.py

I want to get cryptocurrency data from external websocket and save it to database. In order to run producer.py from start, I added it to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
'crypto.spot.producer',
]
producer.py is:
from .models import Spot
async def message(u, t, interval, timeout):
async with websockets.connect(uri=u + '?token=' + t, ping_interval=interval, ping_timeout=timeout) as websocket:
await websocket.send(json.dumps({"id": "4848226",
"type": "subscribe",
"topic": "/market/ticker:all",
"response": True}))
while True:
response = await websocket.recv()
result = json.loads(response)
'''
write result to django model (Spot)
'''
while True:
response = r.post('https://api.kucoin.com/api/v1/bullet-public')
payload = json.loads(response.text)
uri = payload['data']['instanceServers'][0]['endpoint']
token = payload['data']['token']
ping_interval = payload['data']['instanceServers'][0]['pingInterval']
ping_timeout = payload['data']['instanceServers'][0]['pingTimeout']
asyncio.get_event_loop().run_until_complete(message(uri, token, ping_interval, ping_timeout))
asyncio.get_event_loop().run_forever()
However, I get the following error:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\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 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 "C:\Users\a.daghestani\Desktop\marlikfund\crypto\spot\producer.py", line 9, in <module>
from .models import Spot
File "C:\Users\a.daghestani\Desktop\marlikfund\crypto\spot\models.py", line 6, in <module>
class Spot(models.Model):
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py", line 107, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\a.daghestani\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
any idea?
You getting this error because you can not access models before django loading is finished. Your module tries to access models while they are not loaded yet.
To solve your problem you should create a django management command or an standalone script which loads and configures django, after that you can run your producer loop and access models without any problem. e.g.
producer.py modeuls:
import sys, os, django
sys.path.append("/path/to/your/project")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your-project.settings")
django.setup()
from <your-app>.models import Spot
.... Your script
Don't forget to remove the module (crypto.spot.producer) from INSTALLED_APPS.

Categories

Resources