• 6 heures
  • Facile

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 01/03/2022

Discover Virtual Environments

Woman working on a laptop.

What is a virtual environment?

As a Python developer working in a commercial environment, you will often find yourself working on a variety of projects. It is highly likely that each project will make use of a different set of Python packages.

For example, a website that you are working on might make use of the following packages (amongst others):

  • Django==3.0.6

  • requests==2.23.0

  • wagtail==2.9

An alternative data analytics project that you are working on might use these packages instead:

  • numpy==1.18.4

  • pandas==1.0.3

  • requests==2.12.0

Note that the two different projects make use of some different packages, but that they both make use of the  requests  package, albeit with different versions ( 2.23.0  vs  2.12.0 ).

To make sure that you have the correct packages (including specific versions of packages) available to you as part of your local development environment when you switch between projects we use virtual environments.

When you start a project you create a virtual environment. Each virtual environment contains its own Python binary and any Python packages that you choose to install inside it.

Do you have the tools for creating a virtual environment?

We use the Python module  venv  to create and manage virtual environments.  venv  was new in Python version 3.3, so it will be available to you as long as you are using an up-to-date version of Python!

You can check  venv  is available to you by opening your terminal and typing:

→ python -m venv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT]
            ENV_DIR [ENV_DIR...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR       A directory to create the environment in. 

optional arguments:
  -h, --help      show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks      Try to use symlinks rather than copies, when 
                        symlinks are not the default for the platform.
  --copies       Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear       Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade      Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip    Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)
  --prompt PROMPT   Provides an alternative prompt prefix for this
                        environment.

Once an environment has been created, you may wish to activate it, 
e.g. by sourcing an activate script in its bin directory.

If you don’t get the above output (or similar) then you are likely using an outdated version of Python. You can check with:

→python--version
Python3.7.1

If your Python version is < 3.3 you won’t have venv available; you should install the latest version of Python and retry running  python -m venv --help .

If you have followed this chapter successfully then congratulations, you're ready for the next chapter! 😊  If there are any aspects that you are struggling with or want to reinforce then please follow the demonstration in the screencast below:

Let’s Recap!

In this chapter you took your first steps towards working with virtual environments in Python.

  • Virtual environments allow teams of developers to ensure that they are working with the same Python packages on different computers.

  • Virtual environments enable Python developers to use different versions of the same package for different projects.

Now that you’ve checked you have the right tools, you’re ready to create your first virtual environment.

Exemple de certificat de réussite
Exemple de certificat de réussite