Have you ever needed a specific version of python for a project you are working on?
If you’re like me, you are upgrading and using the latest python versions in your code and project to take advantage of the latest language developments. With Python 3.10 being released today, there’s lots to look forward to in each development release — match and case statements being one of the biggest things in this next 3.10 version.
However, sometimes you run across situations where a specific application or dependency you are using has not been updated to support the latest python release. I ran across this the other day using the amazing dbt (data, build, tool). I was installing the latest cli version using their PyPi package in my virtual environment and was unable to do so because Python 3.9.2 caused an error. Rather than go to python.org, download an earlier version, and specify the path to my earlier python executable; I simply deployed a docker container inside VS Code with the version I needed. Here’s how to do it:
- Make sure Docker is installed and running on your machine. If you don’t have it yet, you can download and install it here.
- Once inside your application code base or repo, open up the command palette inside VS Code using cmd + shift + P if you’re on Mac or ctrl + shift + P if you’re on Windows — and start typing “Remote-Containers:”; You should select the “Add Development Container Configuration Files”
2. You’ll see a list of available containers that can be downloaded and deployed inside Docker, which makes this tool even more awesome! You can view all available containers by selecting “View All Definitions”. Select the Python3 container by searching “Python3” in the Command Palette.
3. There you will see a dropdown of available versions to be deployed inside your docker container. In this case, I’ve selected python3.7:
4. Select your version, additionally you have the option to install Node.js in your container at the same time if you’d like, and VS Code will start running the installation of the container. Once the install has completed, you’ll see a new file has been created called devcontainer.json; go ahead and select the Reopen in Container button on the bottom right.
5. VS Code will relaunch your folder which is now running inside your docker container! If you open your terminal inside VS Code, you can see we are now successfully running a dockerized application with a specific python version!
As you can also see, I’m also working inside my main branch; if I switch branches, the branch change will also be reflected inside the Docker CLI:
Hopefully this helps quickly deploy docker containers for code bases requiring specific python versions. This is also a fantastic way for starting projects inside Docker Containers for quicker CI/CD!