Python Virtual Environments CentOS

Just a quick how to for setting up a virtual environment in Linux. This example is using CentOS.

Install Python Virtual Environments

pip install virtualenv

Create Virtual Environment

-bash-4.2$ virtualenv .venv
created virtual environment CPython3.10.2.final.0-64 in 1131ms
  creator CPython3Posix(dest=/home/centos/Scripts/2022/TACACS/.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/centos/.local/share/virtualenv)
    added seed packages: pip==22.2.2, setuptools==65.3.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
-bash-4.2$

Can specify Python version for the virtual env

-bash-4.2$ virtualenv .venv --python=python3.10

Activate the New Virtual Environment

-bash-4.2$ source .venv/bin/activate*
(.venv) -bash-4.2$

Check the Version

(.venv) -bash-4.2$ python --version
Python 3.10.2

Check Python Libraries

This will be empty to being with.

(.venv) -bash-4.2$ pip freeze
(.venv) -bash-4.2$

Install a Library

(.venv) -bash-4.2$ pip install netmiko

Recheck Python Libraries

(.venv) -bash-4.2$ pip freeze
bcrypt==4.0.0
cffi==1.15.1
cryptography==38.0.1
future==0.18.2
netmiko==4.1.2
ntc-templates==3.1.0
numpy==1.23.2
pandas==1.4.4
paramiko==2.11.0
pycparser==2.21
PyNaCl==1.5.0
pyserial==3.5
python-dateutil==2.8.2
pytz==2022.2.1
PyYAML==6.0
scp==0.14.4
six==1.16.0
tenacity==8.0.1
textfsm==1.1.2

Exit Virtual Environment

(.venv) -bash-4.2$ deactivate
-bash-4.2$

Leave a Comment

Your email address will not be published. Required fields are marked *