You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python is a living language which regularly releases new versions. One very big
109
+
transition, which took over a decade, was the switch from version 2 to version
110
+
3 of the language. For many years both versions were in widespread use and
111
+
texts needed to cover their differences. However, Python 2 officially reached
112
+
end of life on 1 January 2020 and not even critical security updates are now
113
+
made to that version. You should, therefore, only ever use Python 3, and all
114
+
references in this book are to that version.
115
+
116
+
Within Python 3, there is a minor version release approximately every year.
117
+
Once released, this receives security updates for 5 years. At the time of
118
+
writing, Python 3.10 is the newest release version, and Python 3.6 is the
119
+
oldest version that still recieves security fixes. The user-facing differences
120
+
between minor Python versions are usually fairly minimal, so for the purposes
121
+
of this book it doesn't matter which of the currently supported versions of
122
+
Python you use.
77
123
78
-
The Faculty of Natural Sciences at Imperial has
79
-
:doc:`centralised instructions for installing all of these tools <fons:index>`, and we'll follow those.
124
+
.. warning::
125
+
126
+
The example code in the exercises uses :ref:`f-strings <tut-f-strings>`
127
+
which were introduced in Python 3.6, so the code will not work in earlier
128
+
versions of Python.
129
+
130
+
Setting up a Python virtual environment
131
+
---------------------------------------
132
+
133
+
.. details:: Video: setting up your virtual environment.
134
+
135
+
.. vimeo:: 486546635
136
+
137
+
.. only:: html
138
+
139
+
Imperial students can also `watch this video on Panopto <https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=011d73de-d93c-4dc8-8996-ac8501521b33>`__
140
+
141
+
In the course of the exercises, You're going to create, edit, and install a
142
+
whole bunch of Python packages. It's highly desirable have a predictable
143
+
programming environment in which the experiments you're doing don't interfere
144
+
with anything else for which you might be using Python, and conversely which
145
+
remains unaffected by any packages you may have installed elsewhere. This
146
+
separation can be achieved by doing everything in a Python :term:`virtual
147
+
environment`, or :term:`venv`.
148
+
149
+
A virtual environment is a folder containing a local installation of Python
150
+
which links back to the Python you installed on your computer. This means that
151
+
virtual environments behave like separate Python installations for most
152
+
purposes, but are fast to install and take very little space because they share
153
+
most of their files with the already installed Python.
154
+
155
+
Creating a working folder
156
+
.........................
157
+
158
+
Start by creating a completely fresh folder for your work on this book. You can
159
+
call this anything you like. On my computer this is called
160
+
:file:`principles_of_programming`. You can create this folder using the
161
+
Windows File Explorer, Mac Finder, or by typing the following in a terminal:
162
+
163
+
.. code-block::
164
+
165
+
$ mkdir principles_of_programming
166
+
167
+
Obviously you replace :file:`principles_of_programming` with whatever you
168
+
decide to call the folder. The dollar sign is the command prompt. Its different
169
+
on some systems, for example, it's often a greater than sign (`>`) or a percent
170
+
symbol (`%`). The text to the left of the command prompt might also be
171
+
different depending on which terminal program you are using on which operating
172
+
system, but we are only concerned with the commands to the right of the prompt.
173
+
174
+
.. hint::
175
+
176
+
Modern operating systems are quite capable of dealing with folder names and
177
+
file names containing spaces. However, there are many pieces of software
178
+
(including some Python packages) that don't correctly deal with spaces in
179
+
folder and file names. It's therefore a safer option to avoid spaces and
180
+
instead to separate words with underscores (:file:`_`).
181
+
182
+
Creating the venv
183
+
.................
184
+
185
+
Now that we have our working folder, we will switch to doing everything in our
186
+
:term:`IDE`, so launch Visual Studio Code. Click on `Open...` in
187
+
the main window or in the `File` menu and select the folder you just created.
188
+
This will open a Visual Studio Code workspace in that folder. You will probably
189
+
be able to see a terminal window at the bottom of the screen. If it's not
190
+
there then open the `View` menu and select `Terminal` to make it appear.
191
+
192
+
The most straightforward way to create a venv is on the terminal
193
+
command line, not from within Python itself. This is accomplished
194
+
using Python's :mod:`venv` package. For example, to create a venv
195
+
called `my_venv` on Windows, you would type:
196
+
197
+
.. code-block:: console
198
+
199
+
> py -m venv my_venv
80
200
81
-
Python
82
-
......
201
+
while on Mac or Linux you would type:
83
202
84
-
Follow the :doc:`FoNS Python instructions <fons:python>`. We will exclusively
85
-
use :ref:`virtual environments <fons:python_virtual_environments>` so it doesn't matter at
86
-
all whether you use Python from Anaconda or from another source. Mac users
87
-
should note, though that the built-in Python will not do, so you should use
88
-
either Homebrew or Anaconda.
203
+
.. code-block:: console
204
+
205
+
$ python3 -m venv my_venv
206
+
207
+
Don't forget that the `>` or `$` stands for the command prompt: you don't
208
+
type it. This command will create the folder `my_venv` and various
209
+
subfolders containing things like the Python program itself and space
210
+
for any packages which you install in the venv. If there was already a
211
+
file or folder called `my_venv` in the current folder then you'll get
212
+
an error, so make sure you choose a new name.
89
213
90
214
.. note::
91
215
92
-
The example code in the exercises uses :ref:`f-strings <tut-f-strings>`
93
-
which were introduced in Python 3.6, so the code will not work in earlier
94
-
versions of Python.
216
+
Running `py` on Windows or `python3` on Mac or Linux is a mechanism to
217
+
attempt to ensure that the right version of Python runs. If you have
218
+
multiple Python installations on your computer then you might end up
219
+
running the wrong one. If this happens then you will need to type the full
220
+
path to the Python you want to use (starting with `/` on Mac or Linux or
221
+
`\` on Windows). Once the venv is installed and activated, it will be
222
+
sufficient to type `python` as the venv will ensure that this is the
223
+
correct version.
95
224
96
-
Git
97
-
...
225
+
A venv doesn't usually contain any particularly valuable data, so you
226
+
should regard them as essentially disposable. In particular, if
227
+
something goes wrong when creating a venv, just delete it and start
228
+
again. In the bash or zsh shells you would type:
229
+
230
+
.. code-block:: console
98
231
99
-
Git is a revision control system. Revision control systems enable you to keep
100
-
track of the different versions of a piece of code as you work on them, and to
101
-
have these versions on different computers as well as backed up in the cloud. We
102
-
will use Git and GitHub classroom as a mechanism for distributing, working with
103
-
and submitting code exercises.
232
+
$ rm -rf my_venv
104
233
105
234
.. warning::
106
235
107
-
When you come to the assessable programming tests that make up 100% of the
108
-
assessment for this module, the code will be distributed and submitted using
109
-
Git. It is therefore essential that you incorporate Git into your day to day
110
-
workflow so that when you come to the test, it's second nature. You will not
111
-
receive marks for test answers that are not committed and pushed.
236
+
`rm -rf` will delete its argument and all its subdirectories
237
+
without further prompts or warnings. There is no undo operation.
238
+
Be very careful about what you delete.
112
239
113
-
Visual Studio Code
114
-
..................
240
+
.. _activate_venv:
115
241
116
-
Visual Studio Code is a Python-aware Integrated Development Environment (IDE).
117
-
This means that it incorporates editing files with other programming features
118
-
such as :ref:`debugging`, Git support, and built-in :ref:`terminal
119
-
<terminal-vscode>`. Visual Studio Code also provides an incredibly useful remote
120
-
collaborative coding feature called Live Share. This will be very useful for
121
-
getting remote help from an instructor.
242
+
Using a venv
243
+
............
122
244
123
-
Setting up a Python environment for this course
124
-
-----------------------------------------------
245
+
If you run Python from the terminal, then the simplest way to use the
246
+
venv is to source its activate script. If using bash or zsh on Mac or
247
+
Linux you would type:
125
248
126
-
.. details:: Video: setting up your virtual environment.
249
+
.. code-block:: console
127
250
128
-
.. vimeo:: 486546635
251
+
$ source my_venv/bin/activate
129
252
130
-
.. only:: html
253
+
while using bash on Windows you would type:
131
254
132
-
Imperial students can also `watch this video on Panopto <https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=011d73de-d93c-4dc8-8996-ac8501521b33>`__
255
+
.. code-block:: console
256
+
257
+
$ source my_venv/Scripts/activate
133
258
259
+
If using PowerShell on Windows then you type:
134
260
135
-
During this course, we're going to create, edit, and install a whole bunch of
136
-
Python packages. In order to have a predictable programming environment in which
137
-
the experiments we're doing don't interfere with anything outside the course for
138
-
which we might be using Python, and conversely to ensure that nothing we've
139
-
installed elsewhere interferes with how we're doing the course, we'll do
140
-
everything in a Python :term:`virtual environment`, or :term:`venv`. You should read up on Python
141
-
virtual environments on the :ref:`Faculty of Natural Sciences Python installation
142
-
page <fons:python_virtual_environments>`.
261
+
.. code-block:: powershell
262
+
263
+
> .\my_venv\Scripts\activate.ps1
264
+
265
+
Obviously, you would use the folder name of your venv instead of
266
+
`my_venv`. In either case, your command prompt will change to indicate
267
+
that you are now using the venv. It might look something like:
268
+
269
+
.. code-block:: console
270
+
271
+
(my_venv) $
272
+
273
+
Any subsequent invocations of Python commands such as `python3` will
274
+
now use the version from the venv, with access to whatever packages
275
+
you have installed in that venv. If you are using a terminal shell
276
+
other than bash or zsh, then see the :mod:`venv` package documentation
277
+
for the correct activation command.
143
278
144
279
.. hint::
145
280
146
-
Don't forget that you need to activate the venv in every new :ref:`terminal <fons:terminal>` session.
281
+
The default permissions settings on your Windows computer may not permit you
282
+
to run the activation script. This can be fixed by running:
0 commit comments