pyenv
Python

Python Version and Environment Manager

Python is an interpreted, object-oriented, high level-programming language that is widely used by network engineers. Python is meant to be a simple and easy to learn language that makes use of high-level data structures for application development, scripting, and gluing scripts together. Python 3 is available by default on Linux distributions.

Before we can get started, we want to setup and make use of virtual environments as a best practice for this lab. Python virtual environments make it possible to install specific packages into an environment that is specific to your application or program. In this way, your packages and requirements will not affect another application or program's packages or requirements. For example, you may require one version of a package or software development kit (SDK), while a different application or program requires a different version of the same package or SDK; this is where virtual environments are beneficial.

We're going to take this one step further with a tool called pyenv. Pyenv not only lets you create and manage virtual environments, but also, different versions of Python if desired. You'll use pyenv to install Python 3 and then create a virtual environment for this lab.

Install and Setup pyenv


Step 1 - Install pyenv Prerequisites

In any distribution, prerequisites are required to be installed. Since Ubuntu is being used for this lab, use apt-get to install pyenv's required prerequisite packages. If asked for a sudo password, enter: cisco.123


sudo apt-get update; sudo apt-get install make tree build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev -y


Step 2 - Install pyenv

Use the pyenv installer tool for ease of installing pyenv:


curl https://pyenv.run | bash


Step 3 - Update .bashrc

After installing pyenv, modify your .bashrc file and source the .bashrc file for your already existing terminal session so that path changes take effect. The following command is using a cat EOF append method to append the PATH variable and pyenv eval commands.


cat <<'EOF' >> ~/.bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)" # This only sets up the path stuff.
eval "$(pyenv init -)" # This makes pyenv work in the shell.
eval "$(pyenv virtualenv-init -)" # Enabling virtualenv so it works natively.
EOF

With that completed you can source the bashrc file to set the terminal variables and be ready to install a Python version and then create your first virtual environment.


. ~/.bashrc

Additional Info

Over the last several years, zsh has become a popular replacement for bash.
If you are using zsh in your own environment, these pyenv commands will need to be placed in your .zshrc file.


Step 4 - Update pyenv

To keep up with the lastest versions of python released, you may need to update the tool by issuing the following command:


pyenv update

Step 5 - Install Python

The command to install a version of Python is pyenv install {python_version}. Install Python 3.11.9 by either typing or copying the command below into your VSCode Terminal window:


pyenv install 3.11.9

Install output:

    Downloading Python-3.11.9.tar.xz...
    -> https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tar.xz
    Installing Python-3.11.9...
    Installed Python-3.11.9 to /home/cisco/.pyenv/versions/3.11.9

NOTE: This process will take a few minutes to complete. Once it is done, you will have Python 3.11.9 installed on your system.

Step 6 - Verify Installed Python Version(s)

You can verify the Python versions you have installed with pyenv using pyenv versions. Check your installed versions by either typing or copying the command below into your VSCode Terminal window and look for 3.11.9 in the output.


pyenv versions

Versions output:

    * system (set by /home/cisco/.pyenv/version)
    3.11.9

Step 7 - Create Directory for Project VirtualEnv

To begin creating a virtual environment or virtualenv, create a directory and change into that directory. Do this by either typing or copying the command below into your VSCode Terminal window:


mkdir ndfclab
cd ndfclab


Step 8 - Create VirtualEnv

The command to create your virtual environment using pyenv is pyenv virtualenv {python_version} {virtualenv_name}. Create a virtual environment called ndfclab using the 3.11.9 Python version you previously installed. Do this by either typing or copying the command below into your VSCode Terminal window:


pyenv virtualenv 3.11.9 ndfclab


Step 9 - Set VirtualEnv in Project Directory

Next, use the pyenv local {virtualenv_name} to set the virtualenv for the project. This will create a .python-version file within the current directory, thus the reason we went ahead and changed directory into the project directory. This file is powerful as it will handle activating and deactiving the virtualenv as you move in and out of the project directory automatically, otherwise, this is an action you would need to perform manually or by some other automated means. Do this by either typing or copying the command below into your VSCode Terminal window:


pyenv local ndfclab

And with that you have created and activated the virtual environment you will use during this lab. Notice that it placed you inside the virtual environment after creating it. The important part is the way the SHELL looks like while you are inside the virtual environment. Notice the (ndfclab) at the start of the line; that indicates the virtualenv is active.

What active virtualenv looks like:

(ndfclab) pod30 ~/workspace/ndfclab $:


Step 10 - Verify Virtual Environment Packages

It is important to make sure you only have two initial packages installed in your virtual environment. You will install more packages later. For now only pip and setuptools should be installed.


pip list


pip list output:

    Package    Version
    ---------- -------
    pip        24.0
    setuptools 65.5.0

Step 11 - Test Package Install using PIP

Pip install the requests library for use later in this lab. Do this by either typing or copying the command below into your VSCode Terminal window:


pip install requests==2.32.2


Step 12 - Verify Package Install

Verify the requests package is installed in your virtualenv.


pip freeze

pip freeze output:

    certifi==2025.1.31
    charset-normalizer==3.4.1
    idna==3.10
    requests==2.32.2
    urllib3==2.3.0

Continue to the next section to get started working with NDFC programmatically.