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 <projectname> Use the virutalenv <projectname> in that folder

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

Leave a Reply

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

*

*