- Check if venv module is installed
python -m venv --help
If venv is not available, we might need to install python3-venv
- Create a Virtual Environment using venv
mkdir VENV
python -m venv VENV
- Activate/Deactivate Environment
source VENV/bin/activate
deactivate
pip install virtualenv
Or
sudo dnf -y install python3-virtualenv #Based RHEL
sudo apt install python3-virtualenv #Debian/Ubuntu
virtualenv <env_name>
virtualenv -p python3 <env_name>
source <env_name>/bin/activate
deactivate
rm -rf <env_name>
-
Use one virtual environment per project.
-
Never commit the virtual environment directory to version control (add it to .gitignore).
-
Use
which python
orwhere python
to verify the active Python interpreter.
pyenv install --list
pyenv install 3.9.6
You can set a global Python version to be used in all shells by running
pyenv global <python_version>
pyenv global 3.9.6
You can set a local Python version for a specific directory by running
pyenv local <python_version>
pyenv local 3.9.6
pyenv versions
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
pyenv virtualenv <python_version> <virtualenv_name>
pyenv virtualenv 3.12.0 myenv
pyenv virtualenvs
pyenv activate myenv
pyenv deactivate