Skip to content

Instantly share code, notes, and snippets.

@alexolinux
Last active June 21, 2025 19:46
Show Gist options
  • Save alexolinux/1758e3562094fa25cdf8f724794d4589 to your computer and use it in GitHub Desktop.
Save alexolinux/1758e3562094fa25cdf8f724794d4589 to your computer and use it in GitHub Desktop.
Python virtualenv by module

Create virtual environment in Python

Virtualenv using python

  1. Check if venv module is installed
python -m venv --help

If venv is not available, we might need to install python3-venv

  1. Create a Virtual Environment using venv
mkdir VENV
python -m venv VENV
  1. Activate/Deactivate Environment
source VENV/bin/activate
deactivate

Python Virtual Environments with virtualenv

Install virtualenv if not already installed

pip install virtualenv

Or

sudo dnf -y install python3-virtualenv #Based RHEL
sudo apt install python3-virtualenv #Debian/Ubuntu

Creating a Virtual Environment with virtualenv

virtualenv <env_name>

Specify the Python interpreter explicitly

virtualenv -p python3 <env_name>

Activating Virtual Environment with virtualenv

source <env_name>/bin/activate

Deactivating Virtual Environment with virtualenv

deactivate

Deleting the Virtual Environment

rm -rf <env_name>

๐Ÿ“š Additional Tips

  • Use one virtual environment per project.

  • Never commit the virtual environment directory to version control (add it to .gitignore).

  • Use which python or where python to verify the active Python interpreter.

Virtualenv using pyenv

List Available Python Versions

pyenv install --list

Install a Specific Python Version

pyenv install 3.9.6

Set a Global Python Version

You can set a global Python version to be used in all shells by running

pyenv global <python_version>
pyenv global 3.9.6

Set a Local Python Version

You can set a local Python version for a specific directory by running

pyenv local <python_version>
pyenv local 3.9.6

List Installed Python Versions

pyenv versions

Use a Specific Python Version

Once you have installed multiple Python versions, you can switch between them using: pyenv global, pyenv local, or pyenv shell commands

pyenv global <python_version>   #sets the global Python version.
pyenv local <python_version>    #sets the local Python version for a specific directory.
pyenv shell <python_version>    #sets the Python version for the current shell session

Manage Virtual Environments with pyenv

Create a Pyenv Virtual Environment

pyenv virtualenv <python_version> <virtualenv_name>
pyenv virtualenv 3.12.0 myenv

List Pyenv Virtual Environments

pyenv virtualenvs

Activate a Pyenv Virtual Environment

pyenv activate myenv

Deactivate a Pyenv Virtual Environment

pyenv deactivate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment