Running Python scripts
In many cases, the best way to run Python programs, is by launching the script (a file with the *.py extension) with Python in Terminal. Note that this is done by calling Python at the bash prompt ('$'), and not within Python ('>>>').
$ python file.py
Some tasks can be done by coding, experimenting and iterating in an editor, such as Atom. When done, we just save the script and run it in Terminal (with the command above).
But for many data collection and analysis tasks, it is recommendable to use Jupyter Notebooks (formerly IPython Notebooks) to write, annotate, and run the code. Such notebooks make things easier to record, understand and reproduce.
To run Jupyter, we must have the package installed.
$ pip install jupyter
If you followed part 3 of this tutorial, you may already have done this.
With Jupyter installed, decide on a folder to run your Jupyter projects in. You can work with Jupyter in several folders, but for the sake of this tutorial, let's have just one single project folder. For example, create a folder called 'jupyter' somewhere on your computer.
To launch Jupyter, we have to open Terminal at the 'jupyter' folder. This can be done in several different ways. A nice way is to set up a keyboard shortcut for it: Head into System Preferences and select Keyboard > Shortcuts > Services. Find "New Terminal at Folder" in the list, tick the box, and set up a key combination. Now, when you're in Finder, just select your folder ('jupyter' in this case), and hit the key command to open Terminal at this folder location.
Whenever you want to work in Jupyter (creating or running code), do the following steps:
- Open Terminal at your desired folder (for example by your keyboard command).
- Enter this:
$ jupyter notebook
Your default browser will open automatically and connect to the Jupyter session you just started at localhost:8888. Make sure to keep the Terminal window running (even if hidden or minimised), otherwise you will have to reconnect. When you are done working, you can close both the browser view and the Terminal session. To go back to the notebook, simply launch it again as described earlier.
We are now in Jupyter, with an empty notebook list.