1 How to work on an exercise from Mac OSX
Julien Palard edited this page 2025-11-14 08:29:03 +01:00

The first exercises were solved directly using the online interactive editor. This is handy but in most real life cases you need to be able to use your own installation of Python and execute scripts on your own computer or server. For this reason, some exercises are meant to be answered by creating and testing a script (i.e. a file, like solution.py) on your computer and then copy and paste its content within the interactive editor on the exercise page.

To do so you need to learn a few things:

  • Install Python on your computer
  • Know basic Terminal usage
  • Create a script and execute it

If this seems trivial to you, just do as you're used to, what matters in what's inside your solution.py :)

Install Python on your computer

We recommend Anaconda Python, a nice packaging of Python with various packages and configurations added keeping scientific usages in mind. Download the Python 3.7 version installer and launch it on your computer. The default proposed options are ok except that you should refuse to install Microsoft VS Code when asked to.

Terminal 101

To launch a Terminal, look for terminal in Spotlight. You should see something like this:

terminal

Welcome to your computer console ! You can do many things from here:

  • know when we're with date
  • know where you're in your computer arborescence with the command pwd
  • create a directory with mkdir
  • move somewhere with cd
  • create a file with touch
  • rename a file with mv
  • list a directory with ls
$ date
Wed Jan 16 16:16:00 CET 2019
$ pwd
/Users/mazr/Downloads
$ mkdir test
$ cd test
$ touch un_fichier.txt
$ mv un_fichier.txt un_autre.txt
$ ls
un_autre.txt

Launch and use Python from a Terminal

Type python and press enter. You should see something like this :

$ python
Python 3.7.1 (default, Dec 14 2018, 13:28:58) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Welcome to the Python interpreter ! A nice place to test some python code ! Type print("Hello") and press Return:

>>> print("Hello")
Hello
>>> import math
>>> print(math.pi)
3.141592653589793
>>>

The get to a new line, hit Ctrl-c. To leave the interpreter and return to your terminal's console, hit Ctrl-d.

Create a Python script and execute it from the Terminal

Install a code editor like Textmate2. Open it and create a new file. Write the following lines:

#!/usr/bin/env python3

print("Hello World !")

(The first line is necessary for your computer to know this is a Python script.)

Save the file as solution.py in your Download folder.

Open a Terminal, go to your Download folder and execute your freshly composed Python script:

$ cd Downloads/
$ python solution.py
Hello World !
$

Congrats ! You can now try to answer your current exercise using your own Python installation and copy and then paste the content of your script in the interactive editor on the exercise page.