kivo
kivo

Reputation: 21

django-pyodbc-azure no module named 'sql_server'

I am attempting to use a corporate SQL Server as the backend for my Django project with Python 3.5.2. I have installed Django 2.0.3, pyodbc==4.0.22, django-pyodbc-azure 2.0.3, pypiwin32==219 (pip freeze below). I have also configured DATABASES in my site's settings.py (see below). When attempting to run the server I get the following error: No module named 'sql_server'. I referenced other questions, the most relevant of which appeared to be: No module named sql_server.pyodbc.base, which concluded that the django-pyodbc-azure and Django versions needed to be identical. This did not solve my problem, however; the same error persists.

Traceback (most recent call last):
  File "manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\K5SH\venv\lib\site-packages\django\core\management\__init__.py"
, line 338, in execute_from_command_line
    utility.execute()
  File "C:\Users\K5SH\venv\lib\site-packages\django\core\management\__init__.py"
, line 312, in execute
    django.setup()
  File "C:\Users\K5SH\venv\lib\site-packages\django\__init__.py", line 18, in se
tup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\K5SH\venv\lib\site-packages\django\apps\registry.py", line 108,
 in populate
    app_config.import_models(all_models)
  File "C:\Users\K5SH\venv\lib\site-packages\django\apps\config.py", line 198, i
n import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\K5SH\AppData\Local\Programs\Python\Python35-32\lib\importlib\__
init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Users\K5SH\venv\lib\site-packages\django\contrib\auth\models.py", lin
e 41, in <module>
    class Permission(models.Model):
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\base.py", line 139
, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\base.py", line 324
, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\models\options.py", line
250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length(
))
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\__init__.py", line 36, in
 __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\utils.py", line 240, in _
_getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Users\K5SH\venv\lib\site-packages\django\db\utils.py", line 129, in l
oad_backend
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an availa
ble database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'base', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: No module named 'sql_server'

Installed packages:

chardet==2.3.0
configparser==3.5.0
cycler==0.10.0
diff-match-patch==20121119
Django==2.0.3
django-crispy-forms==1.7.2
django-csvimport==2.4
django-debug-toolbar==1.9.1
django-import-export==1.0.0
django-money==0.9.1
django-mssql==1.8
django-pyodbc-azure==2.0.3.0
djangorestframework==3.6.3
et-xmlfile==1.0.1
jdcal==1.3
matplotlib==1.5.3
mysqlclient==1.3.12
numpy==1.11.2rc1+mkl
openpyxl==2.4.8
pandas==0.18.1
psycopg2==2.7.3.2
pymssql==2.1.2
pyodbc==4.0.22
pyparsing==2.1.9
pypiwin32==219
pypyodbc==1.3.3
python-dateutil==2.5.3
pytz==2016.6.1
scipy==0.18.1
seaborn==0.7.1
six==1.10.0
sqlalchemy==1.1.4
sqlparse==0.2.4
tablib==0.10.0
virtualenv==15.0.3
xlrd==1.0.0
xlwt==1.2.0

Databases:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'my_db_name',
        'USER': '',
        'PASSWORD': '',
        'HOST': 'my_sql_host',
        'PORT': '',

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

(Note: I have attempted this both with and without the 'OPTIONS' setting).

Upvotes: 2

Views: 2208

Answers (3)

I found this issue mainly due to Version of django I replaced my Django version with django==2.1.15 then it worked. django-pyodbc-azure==2.1.0.0 pyodbc=4.0

Upvotes: 0

xalien
xalien

Reputation: 143

Default virtual environment doesn't work properly on azure. Activate command seems has no effect. But if you create your own virtual environment on azure it works properly. I faced the same kind of problem for my project then I installed virtual environment for myself and edited the auto deployment configurations scripts. Now it is working perfectly.

Upvotes: 0

FlipperPA
FlipperPA

Reputation: 14311

I started fresh on Windows using Python 3.6.0 and virtualenv 15.1.0. These should be close enough that this should work.

First I created and activated a virtualenv, and installed django-pyodbc-azure, which in turn installed Django and pyodbc as dependencies:

virtalenv djangowin
djangowin\Scripts\activate.bat
pip install django-pyodbc-azure

Then I started a fresh Django project:

django-admin startproject sqltest

Then I edited sqltest\sqltest\settings.py and updated my DATABASES definition to use a freshly created database:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'cdw-sql0101.wharton.upenn.edu',
        'PORT': '1433',
        'NAME': 'flipperpa',
        'USER': 'flipperpa',
        'PASSWORD': '',
        'AUTOCOMMIT': True,
        'OPTIONS': {
            'driver': 'SQL Server',
            'unicode_results': True,
            'host_is_server': True,
        },
    }
}

...and it worked like a charm:

(djangowin) C:\Users\tallen\sqltest>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 23, 2018 - 16:39:51
Django version 2.0.3, using settings 'sqltest.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Is there some step along this path that didn't work for you? Are you sure your virtualenv is active? The command djangowin\Scripts\activate.bat is very important - you're going to do that each time you open a new terminal window.

Upvotes: 1

Related Questions