
As a Python developer, managing multiple versions of Python and their associated packages can quickly become a headache, especially when working on different projects with varying dependencies. This is where virtual environments come into play, allowing you to create isolated Python environments for each project and ensuring smooth development without conflicts. In this post, we'll explore how to install pyenv and pyenv-win on Windows 11, powerful tools that simplify the process of managing Python versions and virtual environments. macOS users have an alternative option with direnv
, which is also considered in this blog post.
Windows
Step 1: Install pyenv
. pyenv is a Python version management tool that allows you to install and switch between multiple Python versions seamlessly. To install pyenv on Windows 11, follow these steps:
- Open PowerShell as an administrator.
- Run the following command to 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 the installation completes, restart the PowerShell
Step 2: Install pyenv-win
. pyenv-win is a Python version management extension for Windows that works seamlessly with pyenv. To install pyenv-win, run the following command in PowerShell:
pyenv install pyenv-win
Step 3: Install Python Versions. With pyenv and pyenv-win installed, you can now install different Python versions. To see a list of available Python versions, run:
pyenv install --list
To install a specific version, for example, Python 3.11.5, run:
pyenv install 3.11.5
Step 4: Create and Manage Virtual Environments. Virtual environments are isolated Python environments that allow you to install and manage packages specific to a project without affecting the global Python installation. With pyenv-win, you can create and manage virtual environments effortlessly.
To create a new virtual environment with Python 3.11.5, run the following:
pyenv-venv install 3.11.5 .ultralitics-env
Before installing any Python packages, the virtual environment needs to be activated. To activate:
pyenv-venv activate .ultralitics-env
After executing this command, you should see that the respective env is activated, and there's an indication of that in the CMD (see in green below):
In case you forgot the env name, you can list them using this command:
pyenv-venv list envs
To deactivate the virtual environment, run:
pyenv-venv deactivate
Step 5: Use Virtual Environments. The virtual environment is initially empty. You can check it by running a pip list
(it will only list pip among the packages installed, which is expected). With an active virtual environment, you can install project-specific packages using pip as you normally would. For example, to install the Flask web framework:
pip install flask
macOS
direnv
. This powerful tool allows you to automatically load and unload environment variables based on your current working directory, making it a convenient solution for managing project-specific virtual environments. This page lists an installation guide for macOS and other OS types, including different distributions of Linux.direnv
. First, you'll need to install direnv
using a package manager like Homebrew:brew install direnv
~/.bashrc
file:
eval "$(direnv hook bash)"
~/.zshrc
file:
eval "$(direnv hook zsh)"
~/.zshrc
file:
plugins=(... direnv)
virtualenv
, conda
). For example, with virtualenv
:python -m venv .venv
.envrc
file in your project directory and add the following line to automatically activate the virtual environment when you navigate to the project directory:layout_python-venv python3 .venv/bin/python
direnv
to load the .envrc
file:direnv allow
direnv
will automatically activate the associated virtual environment. When you leave the directory, the environment will be deactivated, ensuring a clean and isolated development environment for each project.Withdirenv
, you don't directly install different Python versions. Instead,direnv
relies on your system's Python installation to create and manage virtual environments. However, you can usedirenv
in conjunction with a Python version manager likepyenv
to seamlessly switch between different Python versions for each project
- Install pyenv:
brew update
brew install pyenv
- Install direnv (as described above)
- Set it up
- Create a new project directory (or use an existing project)
- Install a specific Python version with pyenv. Use
pyenv
to install the desired Python version. For example, to install Python 3.11.5:
pyenv install 3.11.5
- Create a virtual environment. Within the project directory, create a new virtual environment using the installed Python version:
pyenv virtualenv 3.11.5 .ultralitics-env
- Configure direnv. Create an
.envrc
file in your project directory and add the following lines (this line tellsdirenv
to set up the environment using the Python version specified bypyenv
):
layout_python python3 "$(pyenv root)/versions/$(pyenv version-name)/bin/python"
- Allow direnv. Run the following command to allow
direnv
to load the.envrc
file:
direnv allow
Now, whenever you navigate to your project directory, direnv
will automatically activate the associated virtual environment and load the Python version specified by pyenv
. When you leave the directory, the environment will be deactivated. To switch to a different Python version for another project, simply repeat bullet points 3-7 with the desired Python version. Note that direnv
doesn't require any additional plugins to work with pyenv
. It seamlessly integrates with the Python version manager by leveraging the system's Python installation and the virtual environments created by pyenv
.
By combining direnv
and pyenv
, you can manage both virtual environments and Python versions efficiently, ensuring a clean and isolated development environment for each project.
pyenv
and pyenv-win
on Windows 11 and direnv
for macOS, powerful tools for managing Python versions and virtual environments. We covered the installation process, creating and managing virtual environments, and using virtual environments for isolated package installations. By leveraging these tools, you can streamline your Python development workflow, ensure project dependency isolation, and easily switch between Python versions as needed. Say goodbye to the hassle of managing Python environments manually and embrace a more efficient and organized development experience.
Comments