[英]Why do I get "ModuleNotFoundError: No module named 'pyodbc'" using Docker RUN?
I'm creating a Docker container.我正在创建一个 Docker 容器。 To specify python pacakges that need to be installed, I use poetry
pyproject.toml
:要指定需要安装的 python 包,我使用诗歌
pyproject.toml
:
[tool.poetry.dev-dependencies]
pyodbc = "^4.0.32"
...some others...
Everything downloads correctly to venv, but my issue is that when I start my python script with CMD
command in Dockerfile
:一切都正确下载到venv,但我的问题是,当我在
Dockerfile
中使用CMD
命令启动 python 脚本时:
CMD ["/bin/sh", "-c", "python src/test_models.py"]
it works fine, but if i use this RUN
command right before CMD
in my Dockerfile
:它工作正常,但如果我在
CMD
中的Dockerfile
之前使用此RUN
命令:
RUN source ./venv/bin/activate && python src/test_models.py
which does the same thing as CMD
the error occurs:它与
CMD
执行相同的操作,但会发生错误:
...it is not a full output error text, just the meaningful part...
File "/project/venv/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 560, in create_engine
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/project/venv/lib/python3.9/site-packages/sqlalchemy/connectors/pyodbc.py", line 43, in dbapi
return __import__("pyodbc")
ModuleNotFoundError: No module named 'pyodbc'
But what confuses me the most is that if I make CMD
command run a file with an infinite loop and then I go to Docker container's command line and execute the same command that was in RUN
:但最让我困惑的是,如果我让
CMD
命令运行一个无限循环的文件,然后我 go 到RUN
容器的命令行并执行相同的命令:
source ./venv/bin/activate && python src/test_models.py
that works perfectly fine and no pyodbc
error occures.效果很好,并且不会发生
pyodbc
错误。
My Dockerfile
:我的
Dockerfile
:
ARG base_image=gpython:latest
FROM ${base_image}
ARG project_root=/project
COPY entrypoint.sh /usr/bin/
RUN yum install -y gcc-c++ python3-devel unixODBC-devel
RUN chown 1001:0 /usr/bin/entrypoint.sh && chmod u+x /usr/bin/entrypoint.sh
RUN pip install \
--index-url https://artifactory..../pypi/python-remote/simple \
--trusted-host artifactory.... \
--no-cache-dir --upgrade pip poetry && \
mkdir ${project_root} && \
python -m venv ${project_root}/venv && \
chown -R 1001:0 ${project_root}/venv
COPY ./pyproject.toml ./poetry.lock ${project_root}/
WORKDIR ${project_root}
RUN source ./venv/bin/activate && pip install \
--index-url https://artifactory..../pypi/python-remote/simple \
--trusted-host artifactory... \
--no-cache-dir --upgrade pip wheel && \
export REQUESTS_CA_BUNDLE=/etc/...crt && \
poetry config certificates.arti_pypi.cert /etc/....crt && \
poetry install --no-dev --no-root
COPY src ${project_root}/src
COPY models ${project_root}/models
HEALTHCHECK --interval=10s --timeout=1s --retries=3 \
CMD curl -f http://localhost:8080/healthcheck || exit 1
USER 1001
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
#RUN source ./venv/bin/activate && python src/test_models.py
CMD ["/bin/sh", "-c", "python src/test_models.py"]
Agreed with David Maze, you shouldn't need a virtual environment inside a Docker image.同意 David Maze 的观点,您不需要在 Docker 映像中使用虚拟环境。 Try the following:
尝试以下操作:
In a requirements.txt file, have:在requirements.txt文件中,有:
pyodbc~=4.0.32
pyodbc~=4.0.32
Then in your dockerfile have something along the lines of:然后在您的dockerfile中包含以下内容:
FROM python
COPY . .
RUN apt-get update -y && apt-get -y install unixodbc-dev
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["run.py"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.