Python Virtual Environment
2 min read
The tools we are going to use are: VENV and PYENV
venv: Python 3.3 and subsequent versions include a built-in module called venv that enables the creation of virtual environments.
It establishes a fresh and unique Python environment, allowing for the installation and administration of packages for particular projects.
It is straightforward, portable, and simple to use.
Pyenv is a program that enables you to control different Python versions on your computer.
Although it isn't made primarily for building virtual environments, it does let you quickly transition between several Python versions.
Installing Python Versions
Checking for all available versions
pyenv install --list
Installing needed versions
pyenv install 3.10.13
pyenv install 3.11.6
Check For Installed Versions
Find out the root directory where versions and shims are kept:
pyenv version
pyenv root
Create and Activate Environment
/home/<USER>/.pyenv/versions/3.11.0/bin/python3.11 -m venv 'VENV_NAME'
source <VENV_NAME>/bin/activate
Once you run this command, you will have created a virtual environment where every package will use the specified Python version.
From there, you can install your dependencies with pip and start developing in a safe environment.
Add this to your system's PATH:
For Unix/Linux/macOS, open your shell configuration file (
.bashrc
,.zshrc
, etc.) and add:alias python3='/ADD_YOUR_PATH/venv/bin/python3'
Then, source the file:
source ~/.bashrc # or ~/.zshrc
Safe Coding!