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 starting, let's set up a Python virtual environment as a best practice. Python virtual environments enable installation of specific packages into an isolated environment for development of your application or program. This isolation ensures that 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 will leverage Pyenv for managing multiple Python versions and virtual environments. Pyenv not only lets you create and manage virtual environments, but also install 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 Set Up pyenv


Step 1 - Install pyenv

There are several ways to install pyenv. The complete pyenv install guide can be referenced using this link: pyenv installation guide. For ease of installation, this lab leverages the pyenv installer tool:


curl https://pyenv.run | bash


Step 2 - Update .bashrc

After installing pyenv, modify your .bashrc file and source the .bashrc file for your 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 to the .bashrc file.


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

Source the bashrc file to configure terminal variables for Python installation and virtual environment setup.


. ~/.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 3 - Update pyenv

To keep up with the latest released versions of Python, you may need to update pyenv by issuing the following command:


pyenv update


Step 4 - Install Python

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


pyenv install 3.11.14

Install output:

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

Note

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

Step 5 - 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.14 in the output.


pyenv versions

Versions output:

    * system (set by /home/pod30/.pyenv/version)
    3.11.14

Step 6 - 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 ndlab
cd ndlab


Step 7 - Create VirtualEnv

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


pyenv virtualenv 3.11.14 ndlab


Step 8 - Set VirtualEnv in Project Directory

Next, run pyenv local {virtualenv_name} to set the virtual environment for your project. This creates a .python-version file in the current directory—which is why we navigated into the project folder first. This file automatically activates and deactivates the virtual environment as you enter and exit the directory, eliminating the need for manual activation. Enter the command below in your VSCode Terminal:


pyenv local ndlab

Your virtual environment is now created and active. Notice the (ndlab) prefix in your prompt — this confirms the virtualenv is active.

What an active virtualenv looks like:

(ndlab) pod30 ~/workspace/ndlab $:


Step 9 - Upgrade PIP

Go ahead and upgrade pip to the latest version.


pip install --upgrade pip==26.0.1


Step 10 - Verify Virtual Environment Packages

It is important to make sure you only have these 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        26.0.1
    setuptools 79.0.1

Step 11 - Test Package Install using PIP

Install the requests library using pip 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.5


Step 12 - Verify Package Install

Verify the requests package is installed in your virtualenv.


pip freeze

pip freeze output:

    certifi==2026.1.4
    charset-normalizer==3.4.4
    idna==3.11
    requests==2.32.5
    urllib3==2.6.3

Step 13 - Select Python Interpreter in VSCode

One key element when using a virtual environment is to make sure that the IDE is configured to use the virtual environment. This is done by configuring the Python interpreter to use the virtual environment. In Visual Studio Code select the interpreter from the Command Palette.

  1. Click on the gear icon located at the bottom left corner of the IDE
  2. Click on Command Palette
  1. In the search bar, type in python
  2. Click on Python: Set Python Interpreter
    (You may need to scroll or type to find it)
  1. Select the option that is listed as:
    Python 3.11.14 ('ndlab') ~/.pyenv/versions/3.11.14/envs/ndlab/bin/python

Continue to the next section to get started working with Nexus Dashboard (ND) programmatically.