? Tutorial: Understand Virtual environments in Python ✅

Why virtual environments??

Virtual environments are a crucial part on making Python projects, because they allow you to have separated packages for every project you are working on.

It allows us to code with different versions of our needed…


This content originally appeared on DEV Community and was authored by Daniel Diaz

Why virtual environments??

Virtual environments are a crucial part on making Python projects, because they allow you to have separated packages for every project you are working on.

It allows us to code with different versions of our needed packages on every project we are working on, and maintaining the packages of our local machine intact.?

An example could be managing different projects of Django, with different versions like 2.x or the newer 3.x. This could be just because of preferences, or for a business requirement (Since companies usually use older versions of packages).

The cherry on the cake is that Virtual environments allow developers to share the needed packages for the project they are working on, with a simple file normally called requirements.txt and a short command on the terminal.?

So with this brief introduction, let's get into Virtual environments.?

Installation:

Different Virtual environments ?

There are many virtual environment tools for Python, but I'll talk about the main two.

Take into account that these two are really similar so you can easily use one or another.

Virtualenv

The virtualenv package is a full powered tool that allow you to create and manage virtual environments in Python.

It is a external package and you can install it with:

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Python -m venv

Since Python 3.3, an abstracted version of virtualenv is included as a built in package, and therefore it doesn't require any installation.

Differences between virtualenv and built in venv

Virtualenv is a third party package for managing virtual environments, and it requires installation, while venv is a built in virtual environment package which is just a subset of virtualenv, and doesn't require installation.

One of the other main differences are that virtualenv supports Python 2.x and Python 3.x, while the built-in method is just available since Python 3.3.

You can find an extensive differentiation here.

Hands on ?

Now let's use these tools to build what we love, software. ?️

Open your terminal, and type the following commands:

To open a terminal in VS code
image

Click on the top bar Terminal, and click again in New Terminal.

image

  • Virtualenv
virtualenv .venv
Enter fullscreen mode Exit fullscreen mode
  • venv
python3 -m .venv
Enter fullscreen mode Exit fullscreen mode

Where the name .venv, could be any name you want to give to your virtual environment. ?

Note: Remember that no matter what of these two tools you use the following steps will be always the same, the only thing that changes is the creation of the venv.

Venv structure

The structure of a virtual environment is the following

.
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── Activate.ps1
│   ├── easy_install
│   ├── easy_install-3.8
│   ├── pip
│   ├── pip3
│   ├── pip3.8
│   ├── python -> /usr/bin/python
│   └── python3 -> python
├── include
├── lib
│   └── python3.8
│       └── site-packages
│           A lot of packages ...

├── lib64 -> lib
└── pyvenv.cfg

131 directories, 1056 files

Enter fullscreen mode Exit fullscreen mode

What?!

Where did all this stuff come! ?. Well this looks scary but don't worry.

The bin folder contains all the Python binaries and the activation scripts, we will see later how to activate it.

The lib folders contains all of the modules by itself, for example if you are looking for a function definition of a library, probably your editor will redirect you to the lib folder, where all the modules are located.

# looking for the "UserCreationForm" class on Django 

.venv/lib/python3.8/site-packages/django/contrib/auth/forms.py
Enter fullscreen mode Exit fullscreen mode

Activating and deactivating virtual environments

Before we continue I'd like to show you another crucial command while working with virtual environments.

pip freeze
Enter fullscreen mode Exit fullscreen mode

pip freeze will print in the console all the packages available to the Python interpreter you are using.

If you do it in your local machine you will get something like this.


╰─λ pip freeze                                                                                  alembic==1.4.3
apparmor==3.0.0
appdirs==1.4.4
asn1crypto==1.4.0
astroid==2.4.2
attrs==20.3.0
autopep8==1.5.4
Babel==2.8.1
bcrypt==3.2.0
Beaker==1.11.0
blinker==1.4
btrfsutil==5.9
CacheControl==0.12.6
cairocffi==1.2.0
catfish==1.4.13
ceph==1.0.0
ceph-volume==1.0.0
cephfs==2.0.0
cephfs-shell==0.0.1
cffi==1.14.3
chardet==3.0.4
click==7.1.2
colorama==0.4.4
configobj==5.0.6
contextlib2==0.6.0.post1
cryptography==3.2.1
etc ...
Enter fullscreen mode Exit fullscreen mode

Those are all the packages that your Python interpreter has installed (and it's versions). A pretty huge collection it isn't ? ?.

Now let's activate the virtual environment, called .venv

# .venv is the name of the virtual environment
# Activation for a bash shell in Linux or Mac
source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Activating a venv in other shells

Depending of the shell you use, probably you will use a different command, so take a look at the following table.

image

In my case since I use fish as my primary shell, I always use activate.fish.

You will notice a little change in your terminal, and it's because you get a little graphic of what environment are you using.

In my case:

image

After that little parenthesis, run pip freeze again, what do you get?

Exactly, Nothing
Confused

You don't get output, just because the virtual env, has a totally isolated Python interpreter instance, with it's own libraries installed. ?

Deactivating Virtual environments

Deactivate an environment is really easy, just run

deactivate
Enter fullscreen mode Exit fullscreen mode

You will notice that you don't have the name of the venv in your terminal, and if you run pip freeze, you will get all your installed packages.

Installing packages

Before installing any package be sure to have your venv activated!

Depending in which project you are working on, you will want to install the latest and greatest versions, or a specific version.

To install the latest packages with pip just run:

pip install {package}
Enter fullscreen mode Exit fullscreen mode

For example

# Installing it
pip install requests

# pip freeze
certifi==2020.12.5
chardet==4.0.0
idna==2.10
requests==2.25.1
urllib3==1.26.3

Enter fullscreen mode Exit fullscreen mode

Installing a specific version

To install a specific version of a package, run pip install {package name}=={version number}

# Installing Django 2.2

pip install django==2.2

# pip freeze

Django==2.2
pytz==2021.1
sqlparse==0.4.1
Enter fullscreen mode Exit fullscreen mode

Remember that if you want to check the Django version, just invoke python and run the following code.

(.venv)λ python                                                                             
Python 3.8.6 (default, Sep 30 2020, 04:00:38) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
2.2
Enter fullscreen mode Exit fullscreen mode

Installing packages from a text file

To install the packages from a text file just run.

pip install -r {name of the file}.txt
# Usually the file is named requirements.txt
Enter fullscreen mode Exit fullscreen mode

This is extremely useful when you are collaborating in a Python project, and with just a command you get all the necessary stuff to start coding.?

To write all your packages in a file run this command.

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This will generate a file named requirements.txt, which content will be the output of pip freeze.

This way is how most specialized servers install the needed packages, to deploy python apps. You give them a file with the requirements and not the virtualenv itself.

If you want to learn to deploy Django apps, read this tutorial.

Conclusion ?

Now that you know how to use Virtual environments, you can:

  • Separate the requirements for each of your projects
  • Install different versions of packages
  • Collaborate easily since with just a command or two, you can set up your environment
  • Don't mess around the libraries of your machine ?

Commands shortcut ?:

# Install virtualenv, and create one
pip install virtualenv
virtualenv {venv name}

# Built in method (Python 3.3 +)
python -m venv {venv name}

# Activating
source {venv name}/bin/activate

# Deactivating
deactivate

# Install from file
pip install -r {file}.txt

# Write a file
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Follow me in My blog,
to get more awesome tutorials like this one.
Please consider supporting me on Ko-fi you help me a lot to
continue building this tutorials! ?.
ko-fi


This content originally appeared on DEV Community and was authored by Daniel Diaz


Print Share Comment Cite Upload Translate Updates
APA

Daniel Diaz | Sciencx (2021-02-27T02:08:02+00:00) ? Tutorial: Understand Virtual environments in Python ✅. Retrieved from https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/

MLA
" » ? Tutorial: Understand Virtual environments in Python ✅." Daniel Diaz | Sciencx - Saturday February 27, 2021, https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/
HARVARD
Daniel Diaz | Sciencx Saturday February 27, 2021 » ? Tutorial: Understand Virtual environments in Python ✅., viewed ,<https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/>
VANCOUVER
Daniel Diaz | Sciencx - » ? Tutorial: Understand Virtual environments in Python ✅. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/
CHICAGO
" » ? Tutorial: Understand Virtual environments in Python ✅." Daniel Diaz | Sciencx - Accessed . https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/
IEEE
" » ? Tutorial: Understand Virtual environments in Python ✅." Daniel Diaz | Sciencx [Online]. Available: https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/. [Accessed: ]
rf:citation
» ? Tutorial: Understand Virtual environments in Python ✅ | Daniel Diaz | Sciencx | https://www.scien.cx/2021/02/27/%f0%9f%a7%a0-tutorial-understand-virtual-environments-in-python-%e2%9c%85/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.