Home > Net >  Can I import default Python modules in a Python Docker image?
Can I import default Python modules in a Python Docker image?

Time:11-30

I am new to Docker, and as a learning exercise, I want to make a custom Python package available through a Docker image. The package is called hashtable-nicolerg and includes a HashTable class that can be imported with from hashtable_nicolerg.hashtable import HashTable.

It is straightforward to create an image with additional Python packages installed:

  1. Write a Dockerfile
    # Dockerfile
    FROM python:3
    RUN pip install --no-cache-dir hashtable-nicolerg
    
  2. Build the image
    docker build -t python-hashtable .
    

However, the goal, which I realize is hardly an abundant use-case for Docker images, is for the user to be able to create HashTable instances as soon as the container's Python prompt starts.

Specifically, this is the current behavior:

$ docker run -it python-hashtable
Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hash_table = HashTable(capacity=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'HashTable' is not defined
>>> from hashtable_nicolerg.hashtable import HashTable
>>> hash_table = HashTable(capacity=100)

And this is the desired behavior:

$ docker run -it python-hashtable
Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hash_table = HashTable(capacity=100)

I don't want my imaginary users to have to type from hashtable_nicolerg.hashtable import HashTable every time they run a container from this image. So, is it possible for me to effectively run from hashtable_nicolerg.hashtable import HashTable within my Docker image so that users don't have to manually import this module?

Again, I realize this is not the most popular use-case for a Docker image. I'm using this as an exercise to learn more about Python and Docker. I'd appreciate any help!

CodePudding user response:

If the PYTHONSTARTUP environment variable exists and is the name of a valid Python file, then that file will be executed when any new Python shell starts up.

So whether you want to do this in a Docker container or on your local machine, it works the same way. Define PYTHONSTARTUP (in .bashrc, for instance) and then write a startup file.

# .bashrc
export PYTHONSTARTUP=~/.python_startup

 

# .python_startup
from hashtable_nicolerg.hashtable import HashTable

CodePudding user response:

Thanks to Silvio Mayolo for the answer! Here is a Docker alternative to exporting an environment variable from ~/.bashrc:

# Dockerfile
FROM python:3
RUN pip install --no-cache-dir hashtable-nicolerg

WORKDIR /
COPY .python_startup .
ENV PYTHONSTARTUP=./.python_startup

Then after building the image with docker build -t python-hashtable ., I get the desired behavior:

$ docker run -it python-hashtable
Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hash_table = HashTable(capacity=100)
  • Related