RSS Amplifier

Strive2Code · Oct 12, 2023

Managing Python virtual environments

0
Sign in to vote or save

Alex K. · Strive2Code

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 pyenvpyenv 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:

  1. Open PowerShell as an administrator.
  2. 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"
  1. After the installation completes, restart the PowerShell

Step 2: Install pyenv-winpyenv-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:

Read the original on strive2code.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.