Cover image for Python Virtual Environments

Python Virtual Environments

A Python virtual environment is a self-contained “bubble” where you can install and manage Python packages (which are essentially libraries or tools written in Python that you can use in your own projects) without affecting other projects on your computer.

Python Virtual Environment

12 July 2023 by Kevin McAleer | Share this article on


A Python virtual environment is a self-contained “bubble” where you can install and manage Python packages (which are essentially libraries or tools written in Python that you can use in your own projects) without affecting other projects on your computer.


When you’re working on a Python project, you might need to use certain versions of Python packages. However, if you were to install those packages globally (i.e., for the entire system), you might run into issues where one project needs one version of a package, and another project needs a different version.

To prevent these conflicts, you can use a Python virtual environment, which allows you to isolate the Python interpreter, libraries, and scripts used in a specific project. This means you can have multiple environments on your computer, each with different versions of Python and its packages, without them interfering with each other.

Think of it as a sandbox, where you can play around with different packages and their versions without worrying about messing up your other sandboxes.

You create a virtual environment using tools like venv (which is built into Python itself) or virtualenv, conda, pipenv, etc. After creating a virtual environment, you can then “activate” it, which basically means you’re telling your computer to use the Python interpreter and packages from that environment, rather than the global Python interpreter and packages.


Create a Virtual Environment

To create a Virtual Environment, type the following into your terminal:

python3 -m venv venv

Activate the environment

To activate a Virtual Environment, type the following into your terminal:

source venv/bin/activate

Deactivate the environment

To deactivate a Virtual Environment, type the following into your terminal:

deactivate

Using virtual environments is considered a best practice in Python development, because it helps keep your projects organized and prevents issues with package versions and dependencies.


Did you find this content useful?


If you found this high quality content useful please consider supporting my work, so I can continue to create more content for you.

I give away all my content for free: Weekly video content on YouTube, 3d Printable designs, Programs and Code, Reviews and Project write-ups, but 98% of visitors don't give back, they simply read/watch, download and go. If everyone who reads or watches my content, who likes it, helps fund it just a little, my future would be more secure for years to come. A price of a cup of coffee is all I ask.

There are a couple of ways you can support my work financially:


If you can't afford to provide any financial support, you can also help me grow my influence by doing the following:


Thank you again for your support and helping me grow my hobby into a business I can sustain.
- Kevin McAleer