每当我尝试使用connection.cursor时,我都会收到一个错误

时间:2019-10-31 14:36:12

标签: python django postgresql connection

我正在尝试向项目中添加脚本以检查与数据库的连接,但是每次尝试使用“ connection.cursor()”或“ connection.ensure_connection()”时,都会出现错误。知道会是什么。

from django.db import connection

def snapshot():
    try:
        connection.ensure_connection()

    except:
        print('not connected')
    else: 
        print('connected')

snapshot()

Traceback (most recent call last):
  File "Portal/savedb.py", line 82, in <module>
    snapshot()
  File "Portal/savedb.py", line 31, in snapshot
    cursor = connection.cursor()
  File "/usr/local/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 198, in __getitem__
    self.ensure_defaults(alias)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 166, in ensure_defaults
    conn = self.databases[alias]
  File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 147, in databases
    self._databases = settings.DATABASES
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/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 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 'Portal'

0 个答案:

没有答案