You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建Django项目时twisted-iocpsupport编译失败的解决方法

解决Docker容器化Django项目时twisted-iocpsupport编译失败问题

问题场景

将Django项目Docker容器化时,执行docker build -t django_project命令出现编译错误,核心报错为twisted-iocpsupport找不到io.h头文件,导致构建失败。

错误日志

18.82   Building wheel for twisted-iocpsupport (pyproject.toml): started
19.23   Building wheel for twisted-iocpsupport (pyproject.toml): finished with status 'error'
19.24   error: subprocess-exited-with-error

19.24   × Building wheel for twisted-iocpsupport (pyproject.toml) did not run successfully.
19.24   │ exit code: 1
19.24   ╰─> [13 lines of output]
19.24       running bdist_wheel
19.24       running build
19.24       running build_ext
19.24       building 'twisted_iocpsupport.iocpsupport' extension
19.24       creating build
19.24       creating build/temp.linux-x86_64-cpython-39
19.24       creating build/temp.linux-x86_64-cpython-39/twisted_iocpsupport
19.24       gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Itwisted_iocpsupport -I/usr/local/include/python3.9 -c twisted_iocpsupport/iocpsupport.c -o build/temp.linux-x86_64-cpython-39/twisted_iocpsupport/iocpsupport.o
19.24       twisted_iocpsupport/iocpsupport.c:1210:10: fatal error: io.h: No such file or directory
19.24        1210 | #include "io.h"
19.24             |          ^~~~~~
19.24       compilation terminated.
19.24       error: command '/usr/bin/gcc' failed with exit code 1
19.24       [end of output]

19.24   note: This error originates from a subprocess, and is likely not a problem with pip.
19.24   ERROR: Failed building wheel for twisted-iocpsupport
19.24 Successfully built autobahn
19.24 Failed to build twisted-iocpsupport
19.24 ERROR: Could not build wheels for twisted-iocpsupport, which is required to install pyproject.toml-based projects
19.70
19.70 [notice] A new release of pip is available: 23.0.1 -> 23.3.1
19.70 [notice] To update, run: pip install --upgrade pip
------
Dockerfile:13
--------------------
  11 |     # Copy the requirements file into the container and install the Python dependencies
  12 |     COPY requirements.txt /app/
  13 | >>> RUN pip install --no-cache-dir -r requirements.txt --no-dependencies
  14 |
  15 |     # Copy the entire project directory into the container
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install --no-cache-dir -r requirements.txt --no-dependencies" did not complete successfully: exit code: 1

现有配置文件

requirements.txt

asgiref==3.7.2
attrs==23.1.0
autobahn==23.6.2
Automat==22.10.0
cffi==1.16.0
channels==4.0.0
constantly==23.10.4
cryptography==41.0.5
daphne==4.0.0 
Django==4.2.7
hyperlink==21.0.0
idna==3.4
incremental==22.10.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycparser==2.21
pyOpenSSL==23.3.0
python-decouple==3.8
service-identity==23.1.0
six==1.16.0
sqlparse==0.4.4
Twisted==23.10.0
twisted-iocpsupport==1.0.4
txaio==23.1.1
typing_extensions==4.8.0
tzdata==2023.3
zope.interface==6.1

Dockerfile

FROM python:3.9.18

ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE django_project.settings  

WORKDIR /app

COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

COPY . /app/

EXPOSE 8000

CMD ["gunicorn", "django_project.wsgi:application", "--bind", "0.0.0.0:8000"]  

错误原因

twisted-iocpsupport是Windows平台专属的依赖库,用于优化Twisted在Windows系统下的IOCP(输入输出完成端口)性能。而Docker容器使用的是Linux环境,不存在Windows系统的io.h头文件,因此编译失败。Linux环境下Twisted会自动使用epoll等原生IO多路复用机制,完全不需要这个包。

解决方案

步骤1:移除不必要的依赖

编辑requirements.txt文件,删除twisted-iocpsupport==1.0.4这一行,修改后的requirements.txt如下:

asgiref==3.7.2
attrs==23.1.0
autobahn==23.6.2
Automat==22.10.0
cffi==1.16.0
channels==4.0.0
constantly==23.10.4
cryptography==41.0.5
daphne==4.0.0 
Django==4.2.7
hyperlink==21.0.0
idna==3.4
incremental==22.10.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycparser==2.21
pyOpenSSL==23.3.0
python-decouple==3.8
service-identity==23.1.0
six==1.16.0
sqlparse==0.4.4
Twisted==23.10.0
txaio==23.1.1
typing_extensions==4.8.0
tzdata==2023.3
zope.interface==6.1

步骤2:重新构建Docker镜像

执行以下命令重新构建镜像:

docker build -t django_project .

可选优化(非必须)

为了避免类似依赖问题,也可以在Dockerfile中先更新pip,确保使用最新版本安装依赖:
修改Dockerfile中的RUN命令为:

RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt

内容的提问来源于stack exchange,提问作者Denzel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 14:20:42