Dan The Man
Dan The Man

Reputation: 1895

How to Install Private Python Package in Docker image build

my goal is to dockerize python 3.6 web application. During my work I developed private python package ('VmwareProviderBL') that my web application is using.

Build my docker image working perfectly, but when I am trying to run it, I get an error saying my private Python package is not found.

My Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run WebController.py when the container launches
CMD ["python", "WebApi/WebController.py"]

My Error when trying to run the image

Traceback (most recent call last):
  File "WebApi/WebController.py", line 3, in <module>
    from VmwareProviderBL import SnapshotBL, MachineBL, Utils
ModuleNotFoundError: No module named 'VmwareProviderBL'

My package hierarchy

--VmwareVirtualMachineProvider
  --WebApi
  --VmwareProviderBL
  --requirements
  --Dockerfile

Any ideas anyone? I know I need somehow to add this package to my Dockerfile, did not find any example online.

Upvotes: 3

Views: 2772

Answers (1)

larsks
larsks

Reputation: 311238

When you import a module, Python looks in (a) the built-in path (sys.path), (b) locations in the PYTHONPATH variable, and (c) in the directory containing the script you're running.

You're installing the requirements for your module (pip install -r requirements.txt), but you're never installing the VmwareProviderBL module itself. This means it's not going to be available in (a) above. The script you're running (WebApi/WebController.py) isn't located in the same directory as the VmwareProviderBL module, so that rules out (c).

The best way of solving this problem would be to include a setup.py file in your project so that you could simply pip install . to install both the requirements and the module itself.

You could also modify the PYTHONPATH environment variable to include the directory that contains the VmwareProviderBL module. For example, add to your Dockerfile:

ENV PYTHONPATH=/app

Upvotes: 3

Related Questions