简体   繁体   English

为什么使用 Docker RUN 时会出现“ModuleNotFoundError: No module named 'pyodbc'”?

[英]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.

相关问题 为什么在 python 中导入模块时出现“ModuleNotFoundError: No module named 'YEETER'”? - Why do I get “ModuleNotFoundError: No module named 'YEETER'” when importing a module in python? ModuleNotFoundError: No module named” 在 docker 中运行时 - ModuleNotFoundError: No module named” when run in docker ModuleNotFoundError:docker 运行没有名为“请求”的模块错误 - ModuleNotFoundError: No module named 'requests' error for docker run 为什么我在运行 startapp 时收到错误“ModuleNotFoundError: No module named 'django-extensions'” - Why do I get the error "ModuleNotFoundError: No module named 'django-extensions'" when running startapp 尽管使用 pip 安装它,为什么我会收到“ModuleNotFoundError:没有名为‘pyperclip’的模块”? - Why do I get "ModuleNotFoundError: No module named 'pyperclip'" despite installing it with pip? 为什么我在服务器 RedHat 中收到错误 ModuleNotFoundError: No module named 'tkinter'? - Why i get error ModuleNotFoundError: No module named 'tkinter' in server RedHat? ModuleNotFoundError:没有名为“pyodbc”的模块 - Azure 批次 - Ubuntu - ModuleNotFoundError: No module named 'pyodbc' - Azure Batch - Ubuntu Airflow Docker - 没有名为“pyodbc”的模块 - Airflow Docker - No module named 'pyodbc' ModuleNotFoundError:将 pyodbc 导入 py 脚本时没有名为“pyodbc”的模块 - ModuleNotFoundError: No module named 'pyodbc' when importing pyodbc into py script 我收到以下错误:ModuleNotFoundError:未命名模块 - I get the following error: ModuleNotFoundError: No module named
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM