Django ModuleNotFoundError:没有具有Docker的名为“sql\u server”的模块

2024-09-29 18:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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/

requirements.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中配置了数据库(见下文)。尝试运行服务器时,我遇到以下错误:没有名为“sql_server”的模块。我引用了其他问题,其中最相关的问题似乎是:没有名为sql_server.pyodbc.base的模块,该模块得出结论,django pyodbc azure和django版本需要相同。然而,这并没有解决我的问题;同样的错误仍然存在


Tags: installpipdjangopybuildsrctxtapp
1条回答
网友
1楼 · 发布于 2024-09-29 18:39:09

根据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

相关问题 更多 >

    热门问题