python - Django ModuleNotFoundError : No module named 'sql_server' With Docker

标签 python sql-server django docker docker-compose

尝试使用 docker 安装 lib django-pyodbc、django-pyodbc-azure 和 pyodbc 时,我在控制台中遇到错误。如何解决这个问题。

PS C:\Users\TestPIXS\Desktop\Test\Testadmin\master> docker-compose build
Building web
Step 1/7 : FROM python:3
 ---> 28a4c88cdbbf
Step 2/7 : ENV PYTHONUNBUFFERED l     
 ---> Using cache
 ---> aa30b1083d6a
Step 3/7 : RUN mkdir /app
 ---> Using cache
 ---> 1a205fb68d9f
Step 4/7 : WORKDIR /app
 ---> Using cache
 ---> 6f532c2f7d6b
Step 5/7 : COPY requirements.txt /app/
 ---> 1e900bfa67a1
Step 6/7 : RUN pip install  -r requirements.txt
 ---> Running in 08071da8e89b
Collecting Django==3.1.1
  Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)
Collecting django-pyodbc==1.1.3
  Downloading django_pyodbc-1.1.3-py2.py3-none-any.whl (51 kB)
Collecting django-pyodbc-azure==2.1.0.0
  Downloading django_pyodbc_azure-2.1.0.0-py3-none-any.whl (39 kB)
Collecting pyodbc
  Downloading pyodbc-4.0.30.tar.gz (266 kB)
Collecting pytz
  Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting asgiref~=3.2.10
  Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Building wheels for collected packages: pyodbc
  Building wheel for pyodbc (setup.py): started
  Building wheel for pyodbc (setup.py): finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-tiofv2rv
       cwd: /tmp/pip-install-kfqrr62v/pyodbc/
  Complete output (14 lines):
  running bdist_wheel
  running build
  running build_ext
  building 'pyodbc' extension
  creating build
  creating build/temp.linux-x86_64-3.8
  creating build/temp.linux-x86_64-3.8/src
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPYODBC_VERSION=4.0.30 -I/usr/local/include/python3.8 -c src/buffer.cpp -o build/temp.linux-x86_64-3.8/src/buffer.o -Wno-write-strings
  In file included from src/buffer.cpp:12:
  src/pyodbc.h:56:10: fatal error: sql.h: No such file or directory
   #include <sql.h>
            ^~~~~~~
  compilation terminated.
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for pyodbc
  Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pytz, asgiref, sqlparse, Django, pyodbc, django-pyodbc, django-pyodbc-azure
    Running setup.py install for pyodbc: started
    Running setup.py install for pyodbc: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-rhuh80yp/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/pyodbc
         cwd: /tmp/pip-install-kfqrr62v/pyodbc/
    Complete output (14 lines):
    running install
    running build
    running build_ext
    building 'pyodbc' extension
    creating build
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/src
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPYODBC_VERSION=4.0.30 -I/usr/local/include/python3.8 -c src/buffer.cpp -o build/temp.linux-x86_64-3.8/src/buffer.o -Wno-write-strings
    In file included from src/buffer.cpp:12:
    src/pyodbc.h:56:10: fatal error: sql.h: No such file or directory
     #include <sql.h>
              ^~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kfqrr62v/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-rhuh80yp/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/pyodbc Check the logs for full command output.
ERROR: Service 'web' failed to build: The command '/bin/sh -c pip install  -r requirements.txt' returned a non-zero code: 1
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED l
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install  -r requirements.txt
COPY . /app/
要求.txt
Django==3.1.1
django-pyodbc==1.1.3
django-pyodbc-azure==2.1.0.0
pyodbc
设置.py
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASE_ROUTERS = ['Router.Router']
# DATABASE_APPS_MAPPING = {'default':'default','LabDjango':'LabDjango'}
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'TBMS_Mail': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'Test_Mail',
        'HOST': 'TEST01.Test.com',
        'USER': 'Test',
        'PASSWORD': 'PASS2014',
        'trusted_connection': 'no'
        ,
        'OPTIONS': {
        'driver': 'ODBC Driver 17 for SQL Server',
         }
    },     
}
我还在我网站的 settings.py 中配置了 DATABASES(见下文)。尝试运行服务器时出现以下错误:没有名为“sql_server”的模块。我引用了其他问题,其中最相关的似乎是:没有名为 sql_server.pyodbc.base 的模块,得出的结论是 django-pyodbc-azure 和 Django 版本需要相同。然而,这并没有解决我的问题;同样的错误仍然存​​在。

最佳答案

根据https://pypi.org/project/django-pyodbc-azure/ pyodbc是系统依赖。所以你应该看看如何在你的容器中安装它。
我建议在您的 Dockerfile 中添加类似于以下内容的第二步。应该解决它:

RUN apt-get update && apt-get install -y \
    g++ \
    unixodbc-dev
如果没有,请进一步研究此问题:
Unable to install pyodbc on Linux
请记住 python:3图片基于 Debian .

关于python - Django ModuleNotFoundError : No module named 'sql_server' With Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63914506/

相关文章:

python - 执行时间长的 Django 日志查询

javascript - 带有 JQuery 函数的 Django 静态 Javascript 资源

从 Windows 移植到 Linux 时,PHP 对 SQL Server 存储过程的调用不起作用

sql-server - 试图以访问权限禁止的方式访问套接字

c# - 恢复备份时如何更新?

python - Django 获取 sibling 字典而不使用预取相关?

python - Django 在应用程序启动时运行代码但不在迁移时运行

python - 是否可以使用 Spotify Web API 编写没有回调 URI 的桌面应用程序?

Python3,当 UserString 不表现为字符串时?

python - 使新元组/字符串扩展 1 的便捷方法