Nauč se Python > Materiály > Snake Workshop for PyLadies > Preparation > Virtual environment setup

Environment setup #

In this section, you will:

  • prepare a directory for this workshop, and
  • activate a virtual environment.

Preparing a directory #

Programmers create a lot of files. And they care where those files are stored.

The procedure below is by no means the only way to organize files, but if everyone in the workshop uses it, it'll be easier for us to help you with any problems.

A directory for the workshop #

First, create a directory (folder) for the workshop. It can be, for example, learn-python in your home directory. (You can name it differently, but learn-python is used in the examples below.)

You must not move this directory after creating it. Therefore, I do not recommend creating it on the Desktop.

If you ever do move the directory to another location, the virtual environment we will create in a moment will stop working. In that case you will have to delete the environment and create a new one.

At the workshop, you will need to know where this directory is. Note down its full name, which you can then paste into a graphical file browser or into cd in the command line.

We could use the directory directly, but let's organize things a bit more. Create an additional directory inside learn-python, and name it snake-workshop.

Switching to the directory #

Then open the command line and use the cd command to switch to the directory learn-python (i.e. not all the way into snake-workshop). For example:

$ cd learn-python

Then check that you are in the right place:

  • Use the command pwd (on Windows cd) to check that you are really in the newly created directory.
  • Use the command ls (on Windows dir) to check that there is a subdirectory snake-workshop in it.

For example:

Unix (Linux, macOS)

$ pwd

$ ls


> cd

> dir
 Directory of C:\Users\Helena\learn-python
05/08/2014 07:28 PM <DIR>  snake-workshop

Virtual environment #

Now you will create a virtual environment for Python.

A virtual environment is something that ensures that all computers on the workshop will behave roughly the same. Once we set it up, we won't need separate instructions for Linux, Windows, and macOS.

In the future, you will also benefit from the second advantage: each virtual environment is separated from the others, so when you install a library (an extension for Python), it will only affect one virtual environment. If something goes wrong while working on one project, it will not endanger other projects on your computer.

How to do it? It's different on every system!

Linux: #

Depending on how you have Python installed, one of the following commands will work. It will be faster to try them out than to describe which one is correct, so first try:

$ python3 -m venv venv

And if you get the error No module named venv, try instead:

$ virtualenv -p python3 venv

macOS: #

   $ python3 -m venv venv

Windows: #

Depending on how you have Python installed, one of the following commands will work. It will be faster to try them out than to describe which one is correct, so try this first:

> py -3 -m venv venv

And if you get an error like 'py' is not recognized, try instead:

> python3 -m venv venv

That created a directory called venv, which contains the virtual environment. You can look inside, but do not save your files there and never change anything there!

Check that snake-workshop and venv are nicely next to each other.


$ ls


> dir
 Directory of C:\Users\Helena\learn-python
05/08/2014 07:28 PM <DIR>  00
05/08/2014 07:38 PM <DIR>  venv

In a graphical file browser, it looks like this, for example:

(adresáře '00' a 'venv' vedle sebe)

Activation of the virtual environment #

Finally, activate the new virtual environment:


$ source venv/bin/activate


> venv\Scripts\activate

If you use the command line in Visual Studio Code, the command for Windows is more complicated:

> &powershell -ExecutionPolicy bypass
> venv/Scripts/Activate.ps1

After running this command, the word (venv) should appear at the beginning of the command line prompt (before $ or >). This way you will know that the virtual environment is active.

Write down the activation command.

When you open the command line at the workshop, you will need to switch to learn-python using cd and enter this activation command.

Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.