pythonpython-3.xdjangodjango-mssql-backend

Django should connect to SQL Server


I am using PyCharm, Django and the MSSQL-Backend-Project.

I am working on my iMac, and in the local network there is a Microsoft SQL Server I would like to connect to.

I am not sure what credentials to use (IP, oder the HOST named below). However with first attempt I get a hole lot of errors. Which seams for me not to be a problem with the credentials, but with some installation error or something.

I already installed a ne virtual environment (a suggestions I found on google). But still not working.

Can anyone help me, where to start?

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },

    'bnt': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'POS',
        'USER': 'l.bamberg',
        'PASSWORD': '****************',
        'HOST': 'BNTS20005\\SQL17STD',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}

Error message:

Traceback (most recent call last):
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
    import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
    return self.application(environ, start_response)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
    signals.request_started.send(sender=self.__class__, environ=environ)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
    for receiver in self._live_receivers(sender)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
    for conn in connections.all():
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
    return [self[alias] for alias in self]
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
    return [self[alias] for alias in self]
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.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/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
    raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
    import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
    return self.application(environ, start_response)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
    signals.request_started.send(sender=self.__class__, environ=environ)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
    for receiver in self._live_receivers(sender)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
    for conn in connections.all():
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
    return [self[alias] for alias in self]
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
    return [self[alias] for alias in self]
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.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/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
    raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
  Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
  Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET /favicon.ico HTTP/1.1" 500 59```

Solution

  • Just went on as @EgorWexler said and installed the libodbc Driver as followed:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
    brew update
    HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
    

    That worked so far.