Python Virtual Environment Management

PythonPythonBeginner
Practice Now

Introduction

Python virtual environment is a tool used to isolate different Python environments on a single machine. This is useful when working on projects that have different requirements, or when you want to avoid conflicting packages in your global Python environment.

Achievements

  • virtualenv Command
  • Activate Virtual Environment
  • Exit Virtual Environment
  • Assgin Python Version

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") subgraph Lab Skills python/python_shell -.-> lab-96{{"`Python Virtual Environment Management`"}} python/importing_modules -.-> lab-96{{"`Python Virtual Environment Management`"}} python/using_packages -.-> lab-96{{"`Python Virtual Environment Management`"}} python/standard_libraries -.-> lab-96{{"`Python Virtual Environment Management`"}} python/os_system -.-> lab-96{{"`Python Virtual Environment Management`"}} end

Creating Virtual Environment

First, let us create a Python virtual environment.

virtualenv venv

Now we have a virtual environment named "venv". Next, we can install Python packages in the virtual environment.

Python virtual environment creation

Using Virtual Environment

Now, we use the source command to activate the virtual environment:

source venv/bin/activate
Activating virtual environment

Then, we install a third-path package:

pip install pygame
Installing pygame package

pygame is success installed in this environment.

pip list
List of installed packages

Finally, use deactivate command to exit the virtual environment:

Deactivating virtual environment command

We can see pygame is not installed in the main environment.

pygame not in main environment

Different Python Versions

We can also create a virtual environment using the Python version we want.

e.g. create a Python3 virtual environment, first, we must know where the Python3 is:

which python3
Locating Python3 executable path

then we can use -p to assign it:

virtualenv -p /usr/bin/python3 venv3
Creating Python3 virtual environment

Summary

Congratulations! You have completed the Python Virtual Environment Lab.

In this lab, you learned how to:

  • Create a Python virtual environment.
  • Use PyPI package manager to install packages in a Python virtual environment.
  • Manage different Python virtual environments.

Other Python Tutorials you may like