In this guide, I will describe some of the ways you can run Python on your computer. This post is not intended to be a full tutorial. You can find plenty of those on the Internet if you need more details. Instead, I will make some suggestions and point out some helpful resources.
If you’re already comfortable using Python, feel free to continue using whatever setup works for you.
Running Python with Anaconda
Assuming that you’ve set up your Python environment using Anaconda as we’ve suggested, there are several ways to create and run Python code using that environment.
We will start with various ways to run Python using Anaconda Navigator. At the end, you will see ways to run Python from the command line in Mac, Linux and Windows environments.
If you launch Anaconda Navigator and select Home from the left sidebar, your Navigator window should look something like this:
Starting from the Home window, there are several different ways to run Python.
Jupyter Notebook
One of the most popular tools for interactive computing is the Jupyter Notebook. Jupyter Notebook is open-source software that runs in your web browser. You can think of each notebook as a distinct interactive web page.
In Jupyter Notebook, you enter code in input cells, and the results appear in related output cells. The results will usually be in the form of text or graphics (a chart or graph, for example).
Jupyter Notebook can actually run code in over 40 programming languages, not just Python. Other popular languages include R, Julia and Octave.
Notebooks are Documents
However, notebooks can do much more than just run code.
Because notebooks are rendered as HTML, the output can actually be in almost any format a web browser can display. The results could be animated plots, photos, video, audio, or even interactive widgets to display complex objects, such as maps that scroll and zoom.
Jupyter Notebook also allows you to write text in input cells using a markup language called Markdown. Markdown is also commonly used to write text for web pages. By writing text in Markdown input cells, Jupyter Notebook allows you to combine normal text interspersed with your code. The text can even contain mathematical symbols using the LaTeX mathematical typesetting symbols. This is a very useful feature, for instance, if you want to produce a research paper combining mathematical theory, code and results.
Notebooks are integrated documents which can be shared with people or posted to a web page. The document is completely self-contained, with descriptive text, interactive widgets, the code, and the results in one place. This is a major advantage of Jupyter Notebook over most of the other ways to run Python.
Here is a screen shot of an example Jupyter Notebook.
You can access the actual Jupyter Notebook here. Most of our earlier post on web scraping was actually written in a notebook. You can find all of the notebooks used on this site here.
I highly recommend you learn and use Jupyter Notebook as your first choice for sports analytics. If you look at the list of companies and institutions that use Jupyter Notebook, you’ll see it currently includes Google, Microsoft, IBM, Bloomberg, NASA, several other tech companies and a number of major universities. Mastering Jupyter Notebook is time well spent.
Running Jupyter Notebook from Anaconda Navigator
Let’s see how to start running Jupyter Notebook. In the Anaconda Navigator Home window, first make sure that your sports analytics environment is selected in the “Applications on” drop-down menu. Notice that the Home window includes a pane labelled Jupyter Notebook. Click “Launch”, and a new tab will open in your browser. This tab will display the files and folders in the home directory on your computer. Navigate to the folder where you want to open or create a notebook by clicking on it.
If you want to open a notebook that already exists, click on it. To create a new notebook, click on “New” on the upper right of the page, and select your sports analytics environment from the drop-down menu. A new tab will open in your browser with a new blank notebook ready to go. Any Python code you write in your notebook will have access to any packages installed in your sports analytics environment.
Learning Jupyter Notebook
Here are some resources to learn Jupyter Notebook:
Notebook Basics from the official documentation
Introduction to Jupyter Notebooks from Justin Bois of CalTech
Jupyter Notebook Tutorial from DataCamp
Jupyter Notebook tips and tricks from Dataquest
Example Notebooks
Another great way to learn Jupyter Notebook is to see what other people have done. Here are links to some examples.
An example machine learning notebook
A modern guide to getting started with Data Science and Python
A gallery of interesting Jupyter Notebooks
Here’s an entire book on data science in Python (with a focus on the pandas
package written in a series of dozens of Jupyter Notebooks.
Of course, we will be developing a lot of Jupyter Notebooks on this site. Here are all the notebooks from posts used on this site.
QtConsole
Another option from the Anaconda Navigator Home window is to launch Jupyter QtConsole. On the Navigator Home window, this appears in a box labelled “IPy qtconsole”. Clicking “Launch” will bring up a window titled “Jupyter QtConsole”, with a prompt “In [1]:”. This small application looks like a Terminal window, but it’s running an interactive Python called IPython). IPython is actually also running under the hood in Jupyter Notebook as well. IPython is just Python with some interactive features layered on top.
In QtConsole, simply type IPython code after the input prompts. The input prompts all look like “In []:” with a number between the brackets. The result of the IPython command will be displayed immediately below your input.
You can find a tutorial for IPython here. You may find this IPython tutorial useful for working with Jupyter Notebook as well.
In general, I would recommend Jupyter Notebook over QtConsole. Jupyter Notebook is a much richer framework, and you can export, share or publish your work very easily. However, QtConsole is very easy to run and requires virtually no setup. It’s a quick and easy option for simple situations.
Spyder
Anaconda Navigator can also launch Spyder. Spyder is much more complicated than Jupyter Notebook and QtConsole.
Spyder is an _integrated development environment_ ,or IDE.
Spyder provides a range of powerful features:
* Advanced source code editing and testing of large Python programs
* An integrated debugger to help fix errors in your code
* Object inspection so you can examine the values of variables while your program is running
* An interactive console for IPython, similar to QtConsole
An IDE such as Spyder is useful if you want to create large and complex Python projects. Jupyter Notebook is not designed for that purpose. Since Spyder is included in the Anaconda distribution, it’s worth learning if you need to create a full-blown Python program that you intend to package and reuse repeatedly.
It’s nice that Spyder includes an IPython console. If you’re developing a complex project, this makes it easier to try out your code and do analysis without having to jump back and forth to other tools like Jupyter Notebook. After your code is finished and ready to go, you can package it and use it in Jupyter Notebook if you prefer. You’ll see some examples of this in future posts on this site.
Anaconda on Windows
Anaconda looks and feels very similar on Mac and Linux computers. Things are a little different on Windows computers, although the methods described above will work similarly on all platforms. One additional difference with Windows computers is that your Start menu will have an Anaconda folder with items to directly open Jupyter Notebook, QtConsole, Spyder and some other tools. You can use these instead of Anaconda Navigator if you prefer.
Using the Command-Line
There are several other ways to run Python which involve the command line (called Terminal on Mac and Linux computers). These are worth knowing since they are much faster than using the Anaconda Navigator methods described above.
Select Environments on the left-hand sidebar in Anaconda Navigator. You will see a list of Python environments. If you select your sports analytics environment and click on the green arrow to the right of the environment name, a short menu will pop up. On a Mac or Linux computer, this menu has four items:
* Open Terminal
* Open with Python
* Open with IPython
* Open with Jupyter Notebook
Of these options, I think the most useful is Open Terminal.
Terminal
Choose Open Terminal. A new Terminal (command-line session) will start, configured to run Python in your sports analytics environment. More specifically, Open Terminal opens the Terminal set up to run a Python virtual environment. Virtual environments allow you to create distinct environments for each project, without worrying about breaking anything in other projects. This is a major benefit of Anaconda’s environment management tools, as we discussed in our previous post on setting up Python.
In this new Terminal window, you can start up Python in several ways using simple commands such as:
* jupyter notebook
* jupyter qtconsole
* spyder
These commands will open up the applications discussed above, set to run in your sports analytics environment.
The other items from the Anaconda Navigator menu are just shortcuts for things you can do directly in the Terminal. “Open with Python” is the same as entering the command python
in the Terminal. Similarly, “Open with IPython” is the same as entering the command ipython
. Lastly, “Open with Jupyter Notebook” is the same as entering the command jupyter notebook
as we already described. I would recommend you just use the Terminal directly for all of these commands.
Using the Command-Line on Windows
On a Windows computer, you can find the item “Anaconda Prompt” in your Start menu’s Anaconda folder. You don’t even need to use Anaconda Navigator. Choosing “Anaconda Prompt” will open a window with a command line. You can enter the above commands in this window to open the relevant tools such as Jupyter Notebook or Spyder.
IDLE
If you’ve worked with Python before, you probably know a bit about IDLE, Python’s Integrated Development and Learning Environment. IDLE comes installed with the Python version that you can download from the official Python web site. Many people learn Python using IDLE.
Anaconda also includes a version of IDLE which you can use for sports analytics. This can be useful if you want to write longer programs, but don’t want to learn how to use Spyder.
All you need to do is Open Terminal (or “Anaconda Prompt” for Windows) as described above, and enter the command idle3
. This will open up IDLE pointing to your sports analytics environment. You will have access to all the packages you need to do sports analytics.
Other IDEs
Lastly, you can use your sports analytics environment with many other IDEs. I personally like PyCharm, which has a free Community Edition that works very well. You can also use other popular IDEs such as Sublime Text 3, Microsoft’s Visual Studio Code or Atom.
If you decide to use these IDEs, you’ll need to search the Internet for instructions on how to configure the IDE to run Python in your sports analytics environment. This isn’t very hard to do for any of these IDEs, but it is an extra step that varies for each of the IDEs. Since these IDEs are intended as professional-quality software, they include many more features than IDLE. Most of them include automated source code quality checking and version control, and include plugins to incorporate additional features. Unfortunately, as a consequence they require a much larger upfront time commitment to learn how to use properly compared to IDLE or even Spyder.
In Summary
I recommend that you start with Jupyter Notebook, and as you write larger and more complex programs, try Spyder and then maybe one of the other IDEs. Eventually, you’ll develop a workflow that works best for you.