Skip to main content

Python Virtual Environment based on pyenv and venv

·150 words
icysamon
Author
icysamon
Electronics & Creator
Tip

This post was translated by AI.

Installing pyenv
#

Install pyenv, a Python version management tool.

Windows
#

Open PowerShell with Administrator privileges and grant execution permissions.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Install pyenv.

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

After installation, please restart PowerShell.

MacOS
#

Install Homebrew, a package management tool.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install pyenv.

brew install pyenv

Installing Python
#

Install Python.

pyenv install 3.13

You can change 3.13 to any version you prefer.

Set the global Python version.

pyenv global 3.13

Setting up venv Virtual Environment
#

Navigate to your source code directory.

Specify the local Python version.

pyenv local 3.13

Initialize the venv virtual environment.

python -m venv env

Activate the virtual environment.

Windows

.\env\Scripts\Activate.ps1

MacOS

source ./env/bin/activate

If a requirements.txt file exists, install the necessary packages.

python -m pip install -r requirements.txt

The virtual environment setup is now complete.