django-pyodbc-azure没有名为'sql_server'的模块

时间:2018-03-18 17:30:50

标签: django python-3.x pyodbc django-pyodbc django-pyodbc-azure

我试图使用企业SQL Server作为我的Django项目的后端,使用Python 3.5.2。我已经安装了Django 2.0.3,pyodbc == 4.0.22,django-pyodbc-azure 2.0.3,pypiwin32 == 219(下面的pip冻结)。我还在我网站的settings.py中配置了DATABASES(见下文)。尝试运行服务器时,我收到以下错误:没有名为'sql_server'的模块。我引用了其他问题,其中最相关的问题似乎是:No module named sql_server.pyodbc.base,其结论是django-pyodbc-azure和Django版本需要相同。然而,这并没有解决我的问题;同样的错误仍然存​​在。

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'

已安装的软件包:

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 = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'my_db_name',
        'USER': '',
        'PASSWORD': '',
        'HOST': 'my_sql_host',
        'PORT': '',

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

(注意:无论有没有'OPTIONS'设置,我都试过这个。)

3 个答案:

答案 0 :(得分:1)

我在Windows上使用Python 3.6.0和virtualenv 15.1.0开始了。这些应该足够接近,这应该有效。

首先我创建并激活了virtualenv,然后安装了django-pyodbc-azure,后者又安装了Django和pyodbc作为依赖项:

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

然后我开始了一个新的Django项目:

django-admin startproject sqltest

然后我编辑了sqltest\sqltest\settings.py并更新了我的DATABASES定义以使用新创建的数据库:

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,
        },
    }
}

......它就像一个魅力:

(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.

沿着这条道路走了一段不适合你的路吗?您确定virtualenv处于有效状态吗?命令djangowin\Scripts\activate.bat非常重要 - 每次打开新的终端窗口时都会这样做。

答案 1 :(得分:0)

默认虚拟环境在azure上无法正常工作。激活命令似乎没有任何效果。但是,如果您在azure上创建自己的虚拟环境,它可以正常工作。 我为我的项目遇到了同样的问题,然后我为自己安装了虚拟环境并编辑了自动部署配置脚本。现在它工作得很好。

答案 2 :(得分:0)

我发现此问题的主要原因是django版本,我用django == 2.1.15替换了Django版本,然后它起作用了。 django-pyodbc-azure == 2.1.0.0 pyodbc = 4.0