在 django 中安装 django-mssql、pyodbc 时出错

Error installing django-mssql, pyodbc in django

我正在尝试将 mssql-django 软件包安装到我的 EC2(ubuntu) 服务器 这样我就可以将我的应用程序连接到 sql 服务器

但是我在安装 mssql-django 时遇到了问题 pyodbc 留言在下方

    Installing django-mssql...
Adding django-mssql to Pipfile's [packages]...
✔ Installation Succeeded 
Pipfile.lock (2d1a66) out of date, updating to (fad263)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success! 
Updated Pipfile.lock (fad263)!
Installing dependencies from Pipfile.lock (fad263)...
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 1/1 — 00:00:00
An error occurred while installing pyodbc==4.0.32 --hash=sha256:736acad1b264ddb7313058dfe37265b0c5160c1c2a9d1ffd391347c025eb5dd1 --hash=sha256:339d8aa633b0c65be5149c3378c7e3b5bead94dc8bb023a715b416bd047a008e --hash=sha256:bbc07517f339e019ee9f1fe679c4241251d11ca2124567616f67d62e73c29fc0 --hash=sha256:cda790bdc25bfad12d4fb9ba93368275802f7f9ecfa4c9c65e982d3a7fc35f2e --hash=sha256:2152ce6d5131d769ff5839aa762e12d844c95e9ec4bb2f666e8cd9dfa1ae2240 --hash=sha256:6c1e1c1fe747b0f6419e8df0b5c43161e7437dbf72f93f9fcfb9b7358fad3e12 --hash=sha256:56ec4974096d40d6c62a228799122dbc2ade6c4045cc5d31860212a32cae95b1 --hash=sha256:e81ebf9cab80a6eaba7922dea02036e9f8a507a7b818856b8008a02d6fc0d2ab --hash=sha256:c066f032e69fd71e9fadb3a380dfe8ecd1728b40a2bf38f76054d284f8523b29 --hash=sha256:699c080b1c1f7b4afc368b3521fd1161f46a10223443692a249cb01d90949b31 --hash=sha256:0e4178e9b93329bbba17555882008e36a114179d06033b813a13b254dcd755d0 --hash=sha256:9be5f0c3590655e1968488410fe3528bb8023d527e7ccec1f663d64245071a6b --hash=sha256:0d4e14adb149cae45da37fa87aa297055156dae6e89ca3c75493d3d62d78e543! Will try again.
Installing initially failed dependencies...
[pipenv.exceptions.InstallError]: Collecting pyodbc==4.0.32
[pipenv.exceptions.InstallError]:   Using cached pyodbc-4.0.32.tar.gz (280 kB)
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): started
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): finished with status 'done'
[pipenv.exceptions.InstallError]: Building wheels for collected packages: pyodbc
[pipenv.exceptions.InstallError]:   Building wheel for pyodbc (setup.py): started
[pipenv.exceptions.InstallError]:   Building wheel for pyodbc (setup.py): finished with status 'error'
[pipenv.exceptions.InstallError]:   Running setup.py clean for pyodbc
[pipenv.exceptions.InstallError]: Failed to build pyodbc
[pipenv.exceptions.InstallError]: Installing collected packages: pyodbc
[pipenv.exceptions.InstallError]:   Running setup.py install for pyodbc: started
[pipenv.exceptions.InstallError]:   Running setup.py install for pyodbc: finished with status 'error'
[pipenv.exceptions.InstallError]: error: subprocess-exited-with-error
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   × python setup.py bdist_wheel did not run successfully.
[pipenv.exceptions.InstallError]:   │ exit code: 1
[pipenv.exceptions.InstallError]:   ╰─> [10 lines of output]
[pipenv.exceptions.InstallError]:       running bdist_wheel
[pipenv.exceptions.InstallError]:       running build
[pipenv.exceptions.InstallError]:       running build_ext
[pipenv.exceptions.InstallError]:       building 'pyodbc' extension
[pipenv.exceptions.InstallError]:       In file included from src/buffer.cpp:12:0:
[pipenv.exceptions.InstallError]:       src/pyodbc.h:45:10: fatal error: Python.h: No such file or directory
[pipenv.exceptions.InstallError]:        #include <Python.h>
[pipenv.exceptions.InstallError]:                 ^~~~~~~~~~
[pipenv.exceptions.InstallError]:       compilation terminated.
[pipenv.exceptions.InstallError]:       error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
[pipenv.exceptions.InstallError]:       [end of output]
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   note: This error originates from a subprocess, and is likely not a problem with pip.
[pipenv.exceptions.InstallError]:   ERROR: Failed building wheel for pyodbc
[pipenv.exceptions.InstallError]:   error: subprocess-exited-with-error
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   × Running setup.py install for pyodbc did not run successfully.
[pipenv.exceptions.InstallError]:   │ exit code: 1
[pipenv.exceptions.InstallError]:   ╰─> [12 lines of output]
[pipenv.exceptions.InstallError]:       running install
[pipenv.exceptions.InstallError]:       /home/ubuntu/.local/share/virtualenvs/docker_test-M8LOIXqN/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
[pipenv.exceptions.InstallError]:         warnings.warn(
[pipenv.exceptions.InstallError]:       running build
[pipenv.exceptions.InstallError]:       running build_ext
[pipenv.exceptions.InstallError]:       building 'pyodbc' extension
[pipenv.exceptions.InstallError]:       In file included from src/buffer.cpp:12:0:
[pipenv.exceptions.InstallError]:       src/pyodbc.h:45:10: fatal error: Python.h: No such file or directory
[pipenv.exceptions.InstallError]:        #include <Python.h>
[pipenv.exceptions.InstallError]:                 ^~~~~~~~~~
[pipenv.exceptions.InstallError]:       compilation terminated.
[pipenv.exceptions.InstallError]:       error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
[pipenv.exceptions.InstallError]:       [end of output]
[pipenv.exceptions.InstallError]:   
[pipenv.exceptions.InstallError]:   note: This error originates from a subprocess, and is likely not a problem with pip.
[pipenv.exceptions.InstallError]: error: legacy-install-failure
[pipenv.exceptions.InstallError]: 
[pipenv.exceptions.InstallError]: × Encountered error while trying to install package.
[pipenv.exceptions.InstallError]: ╰─> pyodbc
[pipenv.exceptions.InstallError]: 
[pipenv.exceptions.InstallError]: note: This is an issue with the package mentioned above, not pip.
[pipenv.exceptions.InstallError]: hint: See above for output from the failure.
ERROR: Couldn't install package: pyodbc
 Package installation failed...
  ☤  ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/1 — 00:00:07

我在 pipenv 中使用 python 3.8,我想我尝试了一切 包括pip install pyodbc,sudo apt install unixodbc-dev.

请帮忙..

根据微软官方 docs.

您需要从这里安装 ODBC 驱动程序 link

然后运行pip install mssql-django

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 18.04
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 20.04
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 21.04
curl https://packages.microsoft.com/config/ubuntu/21.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 21.10
curl https://packages.microsoft.com/config/ubuntu/21.10/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev

以上是根据document

在Ubuntu上安装ODBC驱动的步骤

在这种情况下,通过执行

解决了问题

sudo apt-get install python3.8-dev