Nov 27, 2018

Managing Multiple Python Instances

The problem with working on different Python codebases is the versioning and the large amounts of libraries available. Say you are working on a data science project using Pandas and then suddenly your boss wants you to take a look at an old Django project that is still using Python 2.7! And what if that messes up how you manage your large MP3 collection? How can you do this? And how you can stop it from looking like a waste site?

While Python 3 has built-in functionality for some of this, it doesn’t help you if have an old Python 2 project to support as well. Luckily, tools for this exist: You can install multiple Python instances with pyenv and you can have different instances of the same version with virtualenv. And pyenv has a plugin for virtualenv so it’s really one-stop shopping.

If you follow the install directions, you will find that you have a command called pyenvLike sdkman for Java, this is the command that will manage all your Python instances. If setup correctly, this will be in control of all your Python environments, executables, and libraries in your user space but not effect the main system version of Python. This way you can keep up with all the latest versions of Python and not rely on your system’s package manager. pyenv will store all your Python instances (“normal” and via virtualenv) in your $HOME/.pyenv/versions folder, which is handy to know if you use something like Pycharm, etc, for your Python development. Just point it to the correct instance under the folder, and you are good to go. Then you can install new libraries via pip in an isolated way, and then switch to another Python instance easily.

Instead of stepping you througbh all the documentation, I thought I would make a little cheatsheet of what the main commands are:

 

Command Result
pyenv install <pyversion> Download, compile and install that version in the pyenv versions
pyenv global see what the global python version is in your user space
pyenv global <pyversion> set the global python version in your user space
pyenv local See the python version local in that folder
pyenv local <pyversion> Set the python version in that folder
pyenv virtualenv <pyversion> <projectname> Create a blank copy (no outside deps) of the python version and install it as the project name (i.e. virtualenv)
pyenv local &lt;projectname&gt; Use the virutalenv <projectname> in that folder

Hopefully this will save you from pulling your hair out while working on different Python projects.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Managing Multiple Python Instances

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Setup Spring DataSource from values stored in AWS Secret Manager
I was tasked migrating an application to AWS recently. The company wanted the application to store the database credentials in AWS’s Secret Manager. The issue that I had is that if I setup a configuration […]
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]