最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - RUN pip install --no-cache-dir -r requirements.txt installing but no working with Docker - Stack Overflow

programmeradmin5浏览0评论

I've trying to use docker for a couple of projects, one is a Django and another is a python telegram bot; But in both cases the problem is that no matter how I copy or install requirements.txt into the container, the libraries apparently get installed, but then all of a sudden I get errors like this in the main python container:

telegram-bot-container | File "/app/run.py", line 15, in <module> telegram-bot-container | import logging, mysql_handler, cmc_handler, constants telegram-bot-container | File "/app/mysql_handler.py", line 2, in <module> telegram-bot-container | from decouple import config telegram-bot-container | ModuleNotFoundError: No module named 'decouple'

And I have to install all missing libraries like this, as if requirements.txt was redundant!:

pip install python-telegram-bot mysql-connector-python python-coinmarketcap python-decouple

Please help me identify the problem.

My whole Dockerfile:

FROM python:3.10-slim

WORKDIR /app

COPY ./requirements.txt /app/

RUN python -m pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt || echo "Skipping problematic package." && \
    pip install python-telegram-bot mysql-connector-python python-coinmarketcap 

COPY . /app

EXPOSE 8081

CMD ["python", "run.py" ]

I tried rebuilding with/without caching. I can see that the packages are being installed in logs.

I've trying to use docker for a couple of projects, one is a Django and another is a python telegram bot; But in both cases the problem is that no matter how I copy or install requirements.txt into the container, the libraries apparently get installed, but then all of a sudden I get errors like this in the main python container:

telegram-bot-container | File "/app/run.py", line 15, in <module> telegram-bot-container | import logging, mysql_handler, cmc_handler, constants telegram-bot-container | File "/app/mysql_handler.py", line 2, in <module> telegram-bot-container | from decouple import config telegram-bot-container | ModuleNotFoundError: No module named 'decouple'

And I have to install all missing libraries like this, as if requirements.txt was redundant!:

pip install python-telegram-bot mysql-connector-python python-coinmarketcap python-decouple

Please help me identify the problem.

My whole Dockerfile:

FROM python:3.10-slim

WORKDIR /app

COPY ./requirements.txt /app/

RUN python -m pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt || echo "Skipping problematic package." && \
    pip install python-telegram-bot mysql-connector-python python-coinmarketcap 

COPY . /app

EXPOSE 8081

CMD ["python", "run.py" ]

I tried rebuilding with/without caching. I can see that the packages are being installed in logs.

Share Improve this question asked Jan 20 at 14:42 AlinAlin 3131 gold badge3 silver badges10 bronze badges 3
  • 2 Could you try to rebuild without the || echo "Skipping problematic package." portion? Perhaps it is obstructing the real problem. – Paolo Sini Commented Jan 20 at 15:07
  • Yes sir, I tried it without that too and it didn't work. – Alin Commented Jan 20 at 15:32
  • With the above portion removed, is the pip install command failing or succeeding? - If it is failing, could you tell us on which package specifically it is failing? - If it is succeeding, could you try rerunning without the second pip install command with the hardcoded libraries and make sure they are also present in the requirements.txt file? You can also run something like docker run -it ${your image name} bash to get an interactive shell into the container and run pip freeze from there to look for discrepencies. – Paolo Sini Commented Jan 20 at 15:41
Add a comment  | 

1 Answer 1

Reset to default 0

I think the following setup would work best for your scenario, where the manual installs and the requirements.txt are separated as different layers which can resolve the issue your facing (which most probably is related to caching)

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

RUN pip install python-telegram-bot mysql-connector-python python-coinmarketcap
发布评论

评论列表(0)

  1. 暂无评论