{%- macro sidebyside(titles=['Unix', 'Windows'], code=True) -%}
In this section, you will:
- prepare a directory for this workshop, and
- activate a virtual environment.
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.
{% set rootname = 'learn-python' %}
First, create a directory (folder) for the workshop.
It can be, for example, {{ rootname }}
in your home directory.
(You can name it differently, but {{ rootname }}
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.
[note] 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 {{ rootname }}
, and name it snake-workshop
.
Then open the command line and use the cd
command to switch to the directory {{ rootname }}
(i.e. not all the way into snake-workshop
).
For example:
$ cd {{ rootname }}
Then check that you are in the right place:
- Use the command
pwd
(on Windowscd
) to check that you are really in the newly created directory. - Use the command
ls
(on Windowsdir
) to check that there is a subdirectorysnake-workshop
in it.
For example:
{% call sidebyside(titles=['Unix (Linux, macOS)', 'Windows']) %} $ pwd /home/helena/{{rootname}}
cd C:\Users\Helena{{rootname}}
dir Directory of C:\Users\Helena{{rootname}} 05/08/2014 07:28 PM
snake-workshop {% endcall %}
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.
[note] 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!
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
$ python3 -m venv venv
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.
dir Directory of C:\Users\Helena{{rootname}} 05/08/2014 07:28 PM
00 05/08/2014 07:38 PM venv {% endcall %}
In a graphical file browser, it looks like this, for example:
{{ figure( img=static('dirs-00.png'), alt="(adresáře '00' a 'venv' vedle sebe)", ) }}
Finally, activate the new virtual environment:
{% call sidebyside(titles=['Unix', 'Windows'], code=False) %}
$ 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
{% endcall %}
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 {{rootname}}
using cd
and enter this activation command.