You open a project to contribute code and quickly realize it requires a specific Python version you don’t have. Not only that, but it’s managing dependencies with some tool you don’t have, like Pipenv or Poetry. What are your options? You could spend a while setting up your machine with a fancy Python development environment , or you could get up and running right away using Docker as a Python version manager - which works the same across MacOS, Windows, and Linux .
👍 Use this Docker strategy if you:
👎 Use the traditional setup with pyenv
if you:
⚠️ If you’re trying to install a CLI tool or script written in Python, you’ll want to explore tools like Pipx . It’s possible to run scripts in containers, but I wouldn’t recommend for everyone.
Why would you bother managing Python versions and environments with Docker when there are tools built to do it for you?
Enough chatter, now shut up and tell me how!
A prerequisite to managing Python versions with containers is to have either Docker or Podman installed on your machine. Once ready, follow the steps below:
#Step 1
git clone <your-repo>.git
cd <your-repo>
# Step 2 & 3: creates container that is destroyed after session
docker run -it --rm --name python-runner -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.7 bash
# OPTIONAL ALTERNATIVE> Step 2,3,& 3a: retain container between sessions
docker run -it --name <project-name> -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.7 bash
# .... many hours later...
docker start -i <project-name>
# Step 4
# install dependencies following the directions based on your project
3.6, 3.7, 3.8, 3.9,
or 3.10
, but you can find every available option on Docker Hub
. Your image will then be python:<chosen-version>
.docker run
command to create a transient container (swap version if needed). The bash
on the end, along with -it
tells Docker to give us an interactive shell, just like when you open a new terminal prompt. The -v
and -w
make the current directory available inside the container, so you’ll need to always run this command from the project.--name
that you will recognize and remove the --rm
(which removes the container when the current run finishes). Now in the future, you can use docker start -i project-name
to pop open the shell, rather than needing to create a brand new container and install dependencies again.Now you’ve got a container running with the Python version the project needs, how can you handle required dependencies? You may need to install a tool inside your running container depending on how the project is configured, directions for the most common options are below. These commands should be run in the container shell created by the docker run
command, not on your local system.
requirements.txt
) 🔗︎Nothing special needed here, run pip install --user -r requirement.txt
and you’re off the the races.
Pipfile.lock
) 🔗︎Couple extra steps needed to get a Pipenv project running.
pip install --user pipenv
# Ensure tool is available in your container's PATH
echo "export PATH=~/.local/bin:$PATH" >> ~/.bashrc
. ~/.bashrc # equivalent to 'source ~/.bashrc'
pipenv install --dev
poetry.lock
) 🔗︎Check the Poetry docs to ensure install command is up to date.
curl -sSL <https://install.python-poetry.org> | python3 -
# Ensure tool is available in your container's PATH
echo "export PATH=~/.local/bin:$PATH" >> ~/.bashrc
. ~/.bashrc # equivalent to 'source ~/.bashrc'
poetry install
run
command to expose the port. Something like -p 8080:80 -p 443:443
is the recommended route to map :. Read more on StackOverflow
.-i,--index-url
.This simpler approach to managing Python versions is another tool in your toolbox, and it can be applied for other languages as well. Hopefully you find success using this to get up and running on projects rather than fiddling with system configurations. Let me know if you have suggestions on ways to make it even simpler and improve it for future users.
Do you want to be more than a code monkey? Learn to avoid blunders and become a superhero to your team.
Finding a solution to parse unescaped double quotes in my JSON strings, with minimal tears.
Tired of hand calculating the total cost for your graduated pricing tier in Stripe? Me, too!