Python – Virtual Environments

A lot of people learning coding by starting with the traditional “hello world” application. I am intrigued that not a lot of time goes into discussing the coding environment setup.

When I have the luxury to work on a greenfields project. I will set the expectations to spend at least 3 weeks geting the “process” right.

Week 1 – Setup the Agile/Kanban board and plan PBI’s around CI/CD, Infrastructure as code.

Week 2 – Development environment setup

Week 3 – Fully automated deployment of a “hello world” application to the cloud. Encompassing – Automatyed Builds, Gated Releases, Centralised Containers (microservices) etc.

Coming back to the development environment. This alone can increase developer productivity by ensuring they are setup correctly. Otherwise they may be spending hours trying to resolve shared package libraries conflicts.

pyenv-virtualenv

pyenv-virtualenv is a pyenv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems.

pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

This project was forked from rbenv and ruby-build, and modified for Python.

It allows us to have complete isolation between projects. So you can focus on code and not package hell. Remember the dll hell days?

// Create Environment
$ pyenv virtualenv 3.7.3 data-collector && pyenv activate data-collector

// Do some Coding!

// Exit virtual environment
(data-collector)$ pyenv deactivate

Now we need to work on another project, we do not need to worry about what data-collector package dependecies are installed, we can just switch to a new environment.

$ pyenv virtualenv 3.7.3 libor-bank-rates && pyenv activate libor-bank-rates

// Do some Coding!

(libor-bank-rates)$ pyenv deactivate

So use pyenv-virtualenv to auto-activate your environments as you work from one project to the next.

$ pyenv virtualenv 3.7.3 data-collector
$ pyenv virtualenv 3.7.3 libor-bank-rates
$ cd ~/data-collector
$ pyenv local data-collector
(data-collector)$ cd ~/libor-bank-rates
$ pyenv local libor-bank-rates
(libor-bank-rates)$ pyenv versions
  system
  3.7.3
  3.7.3/envs/data-collector
  3.7.3/envs/libor-bank-rates
  data-collector
* libor-bank-rates (set by /home/romiko/libor-bank-rates/.python-version)

Also check out Conda.

Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. Conda easily creates, saves, loads and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language

// Create virtual environment
$ conda create --name rangerrom python=3.7.3

// Activate virtual environment
$ conda activate rangerrom

// Exit virtual environment
(ranerrom)$ conda deactivate

Conda will install the version of Python if it isn’t installed. You do not need to run conda install python=3.7.3 first. It has full support for managing virtual environments.

Summary

So the next time you decide you need to wip up some new scripts. Have a good think about how you want the environment to be setup and how package management/dependecies should be handled, before writing the infamous “hello world”.

Check out https://realpython.com for awesome tips, tricks and inspiration.

Advertisement
  • Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s