In this section, you will:
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.
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
Then open the command line and use the
cd command to switch to the directory
learn-python (i.e. not all the way into
$ cd learn-python
Then check that you are in the right place:
cd) to check that you are really in the newly created directory.
dir) to check that there is a subdirectory
$ pwd /home/helena/learn-python $ ls snake-workshop
> cd C:\Users\Helena\learn-python > dir Directory of C:\Users\Helena\learn-python 05/08/2014 07:28 PM <DIR> snake-workshop
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!
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!
venv are nicely next to each other.
$ ls snake-workshop venv
> 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:
Finally, activate the new virtual environment:
$ source venv/bin/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
>). 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
cd and enter this activation command.