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

Docker容器中libpython3.5库找不到,但手动执行正常的问题排查

Fixing Python Version Mismatch in Docker CMD for CNTK Flask Service

Let's break down what's going on here and how to fix it:

Root Cause

When you use docker exec -it <container_id> bash, you're dropping into a bash shell that automatically activates the Anaconda Python 3.5 environment (this is likely configured in the image's .bashrc). That's why manual execution works fine—you're using the correct Python version that matches your compiled .so modules.

But Docker's CMD defaults to running commands with sh instead of bash, and sh doesn't load the .bashrc settings that activate the Anaconda environment. So it's falling back to the system's default Python 2.7, which is incompatible with your Python 3.5-compiled .so files. That's why you saw:

  • First error: libpython3.5m.so.1.0 not found (Python 2.7 was looking for a Python 3.5 library)
  • Second error after adding LD_LIBRARY_PATH: ImportError: dynamic module does not define init function (initcython_bbox)—Python 2.7 expects module init functions named init<module>, but your .so was compiled for Python 3 which uses PyInit_<module> instead.

Solutions

1. Force Docker to use Anaconda's Python directly

The simplest fix is to explicitly call the Anaconda Python interpreter in your CMD. First, confirm the path inside the container (run docker exec <container_id> which python while in the working bash shell—it should be something like /root/anaconda3/bin/python). Then update your Dockerfile:

# Use an official CNTK runtime as a parent image
FROM microsoft/cntk:2.5.1-cpu-python3.5

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Prioritize Anaconda's binaries over system ones (so pip uses the correct version)
ENV PATH="/root/anaconda3/bin:$PATH"

# Upgrade pip (using Anaconda's pip, not system Python 2.7's pip)
RUN pip install --upgrade pip

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py using the correct Anaconda Python
CMD ["/root/anaconda3/bin/python", "test.py"]

2. Run the command via bash to activate the environment

Alternatively, you can tell Docker to use bash and explicitly activate the Anaconda environment before running your script:

# ... (same setup steps as above) ...

# Run using bash to ensure the Anaconda environment is activated
CMD ["bash", "-c", "source /root/anaconda3/bin/activate && python test.py"]

3. Update the PATH to prioritize Anaconda

Adding Anaconda's bin directory to the front of your PATH ensures any call to python or pip uses the Anaconda versions, even in sh shells. This is what the first solution does, and it's the most robust approach because it fixes both python and pip calls during build and runtime.

Why Your Previous Fixes Didn't Work

  • Adding LD_LIBRARY_PATH let Python 2.7 find the Python 3.5 library, but the version mismatch between the interpreter and the compiled module still caused the init function error.
  • Installing libpython3.5 didn't help because the core issue was using the wrong Python interpreter entirely.

内容的提问来源于stack exchange,提问作者Philipp Schäfer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:29:09