[Python] Installing custom pip libraries inside docker container

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am having problems with accessing my app inside Docker container. Basically I want to install it via pip inside the container and run main script that imports this package as dependency.

    Here is Dockerfile main functionality:

    FROM ubuntu
    COPY WasteClassifier /opt/WasteClassifier
    RUN apt update && apt-get -y install sudo && sudo apt-get install -y python3 python3-pip
    RUN export PYTHONPATH="/usr/local/lib/python3.10/dist-packages"
    RUN python3 -m pip install --target="/usr/lib/python3.10/dist-packages" /opt/WasteClassifier
    RUN python3 -c 'import WasteClassifier'


    When running main script from the app I get the following import error:

    ModuleNotFoundError: No module named 'WasteClassifier'


    Made additional step to print all the importables, where my app is listed, although I cannot import it in the same step, here is screenshot of it. Also it is listed in pip libs, pip list screenshot.

    App works correctly outside of container on Ubuntu.

    What might be the problem here? Is it more like Python or Docker issue, where should I look?

    I have tried exporting paths, changing installation paths, changing sys.path in Python, installing different pips (apt-get install pip / apt-get install python-pip), using different Python and Ubuntu versions in the image, none of this works.

    EDIT: Thank you for help, the solution was to reorganize app structure as @yong and @RobD said. Previously it was organized like:

    WasteClassifier
    |-- Dockerfile
    |-- WasteClassifier
    |-- setup.py
    |-- WasteClassifier
    |-- module1
    |-- module 2


    Now it is organized like:

    WasteClassifier
    |-- Dockerfile
    |-- setup.py
    |-- WasteClassifier
    |-- module1
    |-- module2


    Don't know exact reason behind this, and I cannot analyze this right now. If someone is interested I can describe it in detail in a few days. Made also some changes in setup.py and __init__.py, so it might also impact the fix. Thank you for help guys

    Continue reading...

Compartilhe esta Página