How to Use Pip Programmatically in Docker for Installing the ‘debugpy’ Module

The aim of this page📝 is to define steps for programmatic installation of a pip module — specifically debugpy for debugging in a Docker container.

Pavol Kutaj

--

For more, see Debugging for Dockerized ML applications in Python > Sam Watts > Towards Data Science. However, let’s begin with the fact that

…pip is a command line program. While it is implemented in Python, and so is available from your Python code via import pip, you must not use pip’s internal APIs in this way…having said all of the above, it is worth covering the options available if you decide that you do want to run pip from within your program. The most reliable approach, and the one that is fully supported, is to run pip in a subprocess

https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program

  • A simple form is to begin the module with
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])

https://stackoverflow.com/questions/12332975/installing-python-module-within-code

  • You, however, may want first want to check if the module has not already been installed
  • …and install it only as a way of handling the ModuleNotFoundError and proceeding further
import subprocess
import sys
try:
import debugpy
except ModuleNotFoundError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "debugpy"])
import debugpy
debugpy.listen(("0.0.0.0", 5678))
print("Waiting for client to attach...")
debugpy.wait_for_client()
print("hello")
  • Once I run the python script in a fresh container, I get the following output
Collecting debugpy

Downloading debugpy-1.5.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.9 MB)

|█████████████████████████████▎ | 1.7 MB 1.3 MB/s eta 0:00:01

|████████████████████████████████| 1.9 MB 1.3 MB/s

Installing collected packages: debugpy

Successfully installed debugpy-1.5.1

WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

Waiting for client to attach...
  • … happy debugging !

LINKS

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com