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 |1 Answer
Reset to default 0I 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
|| echo "Skipping problematic package."
portion? Perhaps it is obstructing the real problem. – Paolo Sini Commented Jan 20 at 15:07pip 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 secondpip install
command with the hardcoded libraries and make sure they are also present in therequirements.txt
file? You can also run something likedocker run -it ${your image name} bash
to get an interactive shell into the container and runpip freeze
from there to look for discrepencies. – Paolo Sini Commented Jan 20 at 15:41