First steps at network programmability (virtualenv)
In the last years, Software Defined Networks (SDN) and Network Programmability concepts emerged with a lot of frameworks and technologies.
This article focuses on programmability and brings tips for an environment setup.
Looking at the releases of the main Python libraries for network programmability (Figure 1) there’s a lot of activity and three of them with disruptive consequences, some of them listed below:
Figure 1 Libraries releases dates
In that dynamic scenario, the control of the modules’ versions is imperative, and using the wrong one may raise unexpected errors.
Now imagine that you’re following several tutorials, each of them with a different library version, if you aren’t aware of the different releases your script may fail, even with no errors, or if for every tutorial you have to re-install all the libraries your system’s environment or your older script may break.
With that in mind, virtualenv brings flexibility, allowing building a virtual environment for each project, and isolate the modules’ versions per environment.
That separation brings two major benefits, the first one is to segregate the system’s modules from the project’s modules, and the second one is the ability to create a homogeneous environment that can be shared between different machines, maintaining the consistency of the modules’ versioning.
Finally, the installation and use of virtualenv are very simple, you only need to install the module with pip, create the virtual environment, activate the environment, install the project modules and at the end deactivate it.
1. Installing virtualenv
pip install virtualenv
2. Creating a virtual environment
3. Creating a virtual environment
This command creates a virtual environment at folder
4. Activate the virtual environment
cd <virtual environment folder> source ./bin/activate
5. Install modules
pip install module_name # install the latest version pip install module_name==<version> # install the desired version
6. Using the virtual environment
With the virtual environment activated and the modules installed, run your scripts the same way as without the virtualenv.
7. share your virtual environment
After installing all the modules you might want to share that setup and pip freeze prints all the modules that were installed.
pip freeze > requirements.txt
This command generates requirements.txt that contains the environment modules and their versions.
8. Import a virtual environment
With the virtual environment activated runs:
pip install -r requirements.txt
9. Deactivate de Virtual Environment