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.
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
- A simple form is to begin the module with
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
- 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
ModuleNotFoundErrorand proceeding further
subprocess.check_call([sys.executable, "-m", "pip", "install", "debugpy"])
print("Waiting for client to attach...")
- Once I run the python script in a fresh container, I get the following output
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 !