// Creating a Python virtual environment

Creating a Python virtual environment

Python has emerged as the language of choice for network automation. There are a lot of open-source Python tools like Ansible, Nornir, NAPALM, etc… that you can use to build network automation workflows. But managing the various versions of Python and associated package dependencies can be extremely difficult. And if you just use your system python installation, you can create some really nasty headaches for yourself. This is why it is recommended that you leverage Python virtual environments. If you are interested in a more detailed take on why you should use virtual environments, check out some of these articles:


There are multiple options for creating virtual environments, all with their own pros and cons.


In this article, we are going to focus on how to set up a virtual environment using
pyenv, our preferred method. 

Mac OS

On Mac OS you can install pyenv using HomeBrew. Detailed instructions can be found here, but you can just run the following commands.

brew update
brew install readline xz
brew install pyenv

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

exec "$SHELL"


NOTE
: If you use zsh instead of bash, replace .bash_profile in the last command with .zshrc

NOTE: Make sure eval “$(pyenv init -)” is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.

 

Linux (Ubuntu/Debian)

Detailed instructions can be found here, but you can just run these commands in your terminal

sudo apt-get install -y build-essential libssl-dev zlib1g-dev \ 
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ 
libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev \ 
liblzma-dev python-openssl git

curl https://pyenv.run | bash

exec $SHELL

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile

exec $SHELL


NOTE
: If you use zsh instead of bash, replace .bash_profile in the last command with .zshenv


At this point, when you run 
which python in your terminal window it should return

$HOME/.pyenv/shims/python

Now you are ready to install the version of Python you need for your project and create your first virtual environment based on it.

 

  1. Installing a specific Python version
pyenv install 3.7.7
  1. Set your global python version
pyenv global 3.7.7
  1. Create your python virtual environment named test-venv
pyenv virtualenv 3.7.7 test-venv

 

To use your virtual environment simply run pyenv activate test-venv

To verify that your setup was completed properly, when you run pyenv which python you should see

$HOME/.pyenv/versions/test-venv/bin/python

Other useful commands:

  1. To deactivate (or exit out of) a virtual environment
pyenv deactivate
  1. To delete a virtual environment
pyenv uninstall <name_of_virtual_environment>
  1. To list all of your python versions and virtual environments
pyenv versions

 

NOTE: By default your virtualenvs will be in $HOME/.pyenv/versions

If you are using and IDE like PyCharm or VSCode, these links below show you to work with virtual environments:

See What's Possible

Pre-deployment validation of your network’s security, reliability and compliance policies, fully-automated with Batfish.

Request Your Demo