Getting started
Getting started with Python can be a little confusing, hopefully this page helps to get you going.
In general, I am not going to tell you to do things one way. This is because there are a variety of operating systems, and some of you may already have development environments set up in a particular way. The best ways to troubleshoot installation and environment issues are:
- Consult the internet
- Consult a friend/neighbor with the same operating system
Personally, I run Fedora (linux) on my machine. Unless you are as well, you may find that I fall back on steps 1 and 2 above if you ask me for help.
For anyone who is new to Python or maintaining any sort of development environment on a computer, I recommend Anaconda python.
Installing Python
In this class, we will be using Python 3.6. If you want to use a different version, you can try, but the material provided in class may not run with your different version. In particular, if you are using Python 2, you are particularly likely to see things not work. We recommend having some Python 3 interpreter available.
In the terminal, you can check which python version you are currently using with
python -V # -V is the same as --version
You may have a python 2 interpreter as well as a python 3 interpreter. Often these can be invoked as
python2 -V # python 2 version
python3 -V # python 3 version
Following are a variety of methods you can use to install Python. As far as this course is concerned, you can use whatever you want (if you already have system python or anaconda working for you, don't change just for the course).
If you are new to Python, I recommend using Anaconda, regardless of operating system. You can always change your mind later. Conda environments are enough of a reason to at least give it a try.
Anaconda
One convenient method to set up your Python environment is using a free, pre-packaged distribution, such as Anaconda.
This has the advantage that many relevant packages come pre-installed, possibly saving you some headaches later on. It also includes pip, the Python package manager.
Note that while Anaconda also comes with the Anaconda launcher and a bunch of other tools, you should not be using any of these for this course. We only use Anaconda for the convenience of installation of Python and the main packages, not the other stuff that comes with it as well.
If you are a Windows user, Anaconda is recommended, as Windows is notorious for not playing nice with Python.
System Package Manager (Linux)
If you're running Linux, you can just use your package manager (apt-get, dnf, etc.) to install Python. You can also often use your package manager to install Python packages instead of pip, but you may run into issues doing this.
Homebrew (Mac only)
If you want a bit more control over your Python distribution, then using Homebrew to install Python is useful.
It also installs the package manager pip for you, which is very useful.
However, it does not come with additional modules, such as numpy or scipy, though they can be installed easily using pip.
A short tutorial can be found here.
Note to Windows Users
If you are a Windows user and are serious about taking this and other computer classes, I recommend using the Windows Subsystem for Linux. In general, much scientific computing is done on clusters running some form of Linux. The sooner you start using Linux, the better.
If you really want to take the plunge, you can also partition your hard drive and add a linux distribution as a second operating system.
If the install guides for Anaconda fail, and some Googling leads to nothing of utility, I highly suggest running with a cloud-based option.
Editor
To write code, you need an editor. While there are many options, old and new, Atom is (generally) what I use.
A script is a simple file with text, such as
a = 'Hello,'
b = 'world!'
print(a + ' ' + b)
which you can save to your filesystem using the editor.
Running code
Now that you know how to write Python scripts, it's time to learn how to run them using Python.
Linux
Open a terminal.
To open the interpreter, enter python
To run a script enter python <script>.py
where <script>.py
is the path to the script.
Mac
Open the terminal, for example by searching for terminal using the spotlight search function.
To open the interpreter, simply enter python (this is the interactive mode).
To run a script, navigate to the folder that contains your script. Then simply run python script.py
, given that script.py
is the name of your script.
Online, you can find some good resources that introduce the terminal, such as
- How to Use Terminal: The Basics
- Navigating the Terminal: A Gentle Introduction
- 25 Terminal Tips Every Mac User Should Know
- Getting started with Linux, Section 3 and beyond (Note that Linux and Mac have the same terminal)
- Stanford course CS 1U 'Practical Unix'
Windows
Open the command prompt by searching for cm
+ in the Start menu.
This is the Windows version of the terminal.
To open the interpreter, similar to the Mac terminal, we simply run +python+.
To run a script, navigate to the folder that contains your script, and then run python script.py
, given that script.py
is the file name.
For Windows, there are some good resources about the command prompt
If you are using Windows and you're having trouble setting up Python, I recommend using Python in the cloud, as I'll discuss later.
Pip
To install new modules, make sure you have pip
installed.
It comes with both Anaconda and Homebrew, so if you have used either of them, you are good.
Otherwise, it is also rather straightforward to install pip
: See the documentation.
Then, open the terminal / command prompt, and run pip install module
, where module
is the name of the module.
Note that you cannot run this from inside the Python interpreter.
Python in the cloud
When you have trouble getting Python up and running, or if you have Windows and would like to use a Unix environment, then there is an alternative: using your browser.
One option is AWS.
If anyone has other suggestions, I am open to them.
Farmshare
Another option available to everyone is farmshare. You simply need to ssh
in, and you will have a full working installation of python. This requires some Linux skills, so follow this guide to getting started.