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
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]