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 is available by default on Linux distributions. This can come as a Python 2.7 version or more than likely nowadays, a Python 3 version since Python 2.7.18 was the last release of Python 2.
To get started, you want to setup and make use of a virtual environment 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 your virtual environment.
In any distriubtion, prerequisites are required to be installed. Since Ubuntu is being used for this lab, use apt-get
to install pyenv's required prerequisiste 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
The pyenv installer tool will be leveraged to ease the install of pyenv. The alternative is to clone the pyenv repository and install it from source.
curl https://pyenv.run | bash
After installing pyenv, modify your .bashrc
file and source the .bashrc
file so that path changes take effect.
The following command will 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 your your .bashrc
file updated, you can source it to set the terminal variables and be ready to install
a Python version.
. ~/.bashrc
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.
To keep up with the lastest versions of Python released, you may need to update the tool by issuing the following command:
pyenv update
The command to install a version of Python is pyenv install {python_version}
.
Install Python 3.11.6 by either typing or copying the command below into your VSCode Terminal window:
pyenv install 3.11.6
Install output:
Downloading Python-3.11.6.tar.xz... -> https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tar.xz Installing Python-3.11.6... Installed Python-3.11.6 to /home/cisco/.pyenv/versions/3.11.6
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.6
in the output.
pyenv versions
Versions output:
* system (set by /home/cisco/.pyenv/version) 3.11.6
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:
cd /home/cisco/Documents/
mkdir nxapilab
cd nxapilab
The command to create your virtual environment using pyenv is pyenv virtualenv {python_version} {virtualenv_name}
.
Create a virtual environment called nxapilab
using the 3.11.6
Python version you previously installed.
Do this by either typing or copying the command below into your VSCode Terminal window:
pyenv virtualenv 3.11.6 nxapilab
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 nxapilab
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 (nxapilab)
at the start of the line; that indicates the virtualenv is active.
What active virtualenv looks like:
(nxapilab) cisco@ubuntu-vm-03:~/Documents/nxapilab$
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 23.2.1 setuptools 65.5.0 [notice] A new release of pip is available: 23.2.1 -> 24.0 [notice] To update, run: python3.11 -m pip install --upgrade pip
Go ahead and upgrade pip to the latest version.
pip install --upgrade pip==24.0
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.31.0
Verify the requests package is installed in your virtualenv.
pip freeze
pip freeze output:
certifi==2024.2.2 charset-normalizer==3.3.2 idna==3.6 requests==2.31.0 urllib3==2.2.0
Continue to the next section for a detailed overview of NX-API.