180A Python Setup
∗
Dylan Neff
January 7, 2021
Part I
Introduction
This document explains how to download and install the Anaconda distribution for Python and use
the Spyder IDE (integrated development environment) to edit and run Python code. While Spyder
is the focus of this document, there are many other perfectly acceptable options for editing and
running Python code. Two such options included in the Anaconda distribution are the PyCharm
IDE and Jupyter Notebook. If any of this material is out of date/incorrect, please email the issue
to Dylan Neff.
Note
As of December 2020, a stand-alone version of the Spyder IDE can be downloaded from
Spyder’s website and installed without the rest of the Anaconda distribution. This allows
for a smaller computer storage footprint and should work the same as the Anaconda version
of Spyder, though it was not explicitly tested by the author. This version of Spyder is
available for Windows and Mac (not Linux).
Part II
Installing Anaconda
Anaconda is a data science distribution for Python and R. It contains a handful of useful software
packages and the ability to manage environments with conda (language-agnostic package manager).
For practical purposes, downloading and installing this distribution will allow the user to write/run
Python code as well as manage Python packages. There may be variations between operating
systems for the installation and use of the Anaconda distribution but, as of 2021, Anaconda is
actively maintained and accessible to Windows, Mac, and Linux.
∗
Physics Department, UCLA, Los Angeles, CA 90024, USA
1
1 Downloading the Anaconda Distribution
To download the Anaconda distribution:
• Go to Anaconda’s site
• Scroll to ‘Anaconda Installers’ (typically found at the bottom of the page)
Figure 1: Anaconda’s download page.
• Select the operating system of the machine you want to install to
• Click either the 32 or 64 bit version. The author would recommend installing the 64-bit
version of Python 3 (3.8 at the time of writing). Upon clicking this link, the file should begin
downloading in the browser (this may take a minute or longer, depending upon internet
speed).
2 Installing the Anaconda Distribution
Once the installer has finished downloading, the Anaconda Distribution is ready to install. The
installation process will differ between operating systems.
2.1 Windows
In Windows an executable file should have been downloaded. Run this executable (choosing to trust
it if Windows should ask) and follow the instructions on the installer. The author recommends
installing for ”Just Me” as opposed to ”All Users” and selecting to ”Register Anaconda as my
default Python 3.#”. Continue following the installer’s directions.
2
2.2 Linux
In Linux a bash script will be downloaded. Either make the file executable or go to terminal and
execute bash Anaconda3.### in the directory containing the downloaded bash script. Follow
the installer instructions from the command line. In Linux you will need to ensure that the base
environment for Anaconda is activated before using anaconda in a terminal. To activate the base
environment, type ”conda activate base”.
2.3 Mac
On Mac a .pkg file will be downloaded. Double click this downloaded .pkg file to start the installer
and follow instructions. The author would recommend installing locally.
Part III
Spyder
Spyder (Scientific PYthon Development EnviRonment) is an IDE built to support data science. It
contains a text editor, a terminal, a debugger, and much more. The benefit of Spyder (and IDEs in
general) is the ability to do everything from a single application. Many text editors have evolved
to match the functionality of pre-packaged IDEs and usually have superior customizability. The
strength of a pre-packaged IDE such as Spyder is the number of features immediately offered after
installation. There are options for customization but, out of the box, all of the necessary tools are
available. This is a huge advantage for the inexperienced user trying to get started in Python.
1 Starting Spyder
The process of starting Spyder will be slightly different for different operating systems and plat-
forms. However, once the Anaconda distribution has been installed, it should not be too difficult
to start Spyder.
Once Spyder starts up, it may present a notification that a newer version is available. Upgrading
Spyder through conda will not be explained in this document but is very Google-able (upgrading
is not necessary but may be helpful to have the latest version).
You may also be asked to install ‘Kite’ which allows for smart code completion. It is useful but
only a very limited version is available without upgrading to the paid version.
1.1 Windows
On Windows 10, for instance, type “Spyder” into the Cortana search bar and the application should
be the first result. In general, the shortcut to the Spyder executable will be in the Anaconda3
application folder.
1.2 Linux
In Linux, Spyder will need to be started from the command line. Open a terminal and make sure
a conda environment is active (”conda activate base” to activate base). If a conda environment
containing Spyder is active, simply type ”spyder” to start the IDE.
3
1.3 Mac
On Mac, open the Launchpad and start the new Anaconda Navigator application. From here, find
the Spyder block and click Launch to start the Spyder IDE.
2 Spyder Interface
Once Spyder is launched, the main Spyder interface will be disypayed - simar to Figure 2. A window
offering a tutorial may pop up. Working through this quick introduction may be useful, even if
you don’t fully understand the explained features. While there are many features in this interface,
only the bare necessities will be introduced.
The main text editor is usually placed in the middle to left side of the screen and is labeled
with a 1 in Figure 2. This is where files stored on the computer can be edited. This text editor
comes with features such as auto-complete, Python highlighting, tool-tips, and more that can be
Googled. It has most of the same shortcuts as other text editors (Microsoft Word, whatever the
equivalent is on Mac). Here you’ll type your code, save, and edit.
In addition to the text editor, there is also a console in the bottom right corner labeled 2. This
IPython console behaves like a python interpreter. Typing Python commands here and hitting
enter will evaluate the command and produce the result expected for the Python language. This
is an excellent area to quickly test code or even run some very short analyses.
3 Running in Spyder
For larger projects/analyses, it is highly recommended to work from the text editor and then run
the subsequent .py file. To run the active .py file in the text editor, click the green run arrow in
the toolbar (circled in Figure 2). Upon clicking this run arrow for the first time, a dialogue will
appear as shown in Figure 3 (this dialogue can be opened at any time by clicking the “Run” tab
from the top toolbar and selecting ”Configuration per file...”). The author would suggest using the
same settings shown in Figure 3.
Once these settings are configured, click “Run” at the bottom of the dialogue box. This will
open another tab in the console (if the settings shown in Figure 3 are used) and run the code in
the file. Figure 4 shows the result for a simple Hello World program.
4
Figure 2: Spyder interface.
Figure 3: Spyder run dialogue.
5
Figure 4: Spyder run result.
Part IV
Python
Python is a high level interpretive language that values readability over speed. Python programs
run slower than other lower level compiled languages such as C++ but what Python lacks in speed
it makes up for in simplicity. Python code, when written well, is much easier to read and understand
than other languages due partially to it’s stress on implicit typing. This along with other factors
allows programmers to more quickly produce working code (rapid prototyping) that, in many cases,
may make up for the fact that the code itself will run rather slow.
In the author’s opinion, the greatest strength of Python is its online community. The resources
available online for solving common problems in Python are extensive. Many new programmers
choose Python as their first language and many experienced software engineers employ Python in
modern analysis and applications. This leads to online resources at every step along the learning
curve and beyond. It is a rare occasion to find a problem in Python that cannot be solved by a
simple Google search.
In addition, due to the open source nature of Python along with its simple package management
system, there is an astounding amount of software already available for use. Many of these packages
are extremely easy to simply plug and play. If directions between two locations were ever needed
for an application, for instance, there is a Google maps package that can be installed and planning
a route in a matter of minutes. These resources allow the user to complete complex tasks with
minimal effort, leaning on the past work of the open source community.