Python Installation and Usage Guide

August 20, 2018

Contents

1 Overview 2

2 Windows 10 3 2.1 Installing Python ...... 3 2.2 Using Python ...... 4 2.2.1 Writing Code ...... 4 2.2.2 Using the PowerShell ...... 5 2.3 Optional: Choosing and Customizing a Text Editor ...... 7 2.4 Optional: Subsystem on Windows ...... 8 2.5 Optional: Installing and Using Spyder ...... 9 2.6 Other Options ...... 9

3 Linux () 9

4 Mac OSX 10

5 Running in a lab/classroom 11

6 ALL OS - Installing Packages - VERY IMPORTANT 11 6.1 On Linux ...... 11 6.2 Installing a package (DO BEFORE HW 1) ...... 12

1 1 Overview

Welcome to Mathematical Physics! To get started we will get setup and com- fortable using Python. Python is widely used and because of this, we should have little to no problem running it in the lab and on our own machines. There are sections in this document for Windows 10, Mac OSX, and Linux (Ubuntu) installation and usage instructions - as well as how to run our pro- grams on campus machines with what is already installed. Some optional details are included in the Windows section which you may find fit your work flow or are easier to use. (The text editor section actually can apply to all OSes).

We want everyone to build comfort with using a terminal. While there are lots of IDEs (pretty and easy to use development environments) out there, a goal of this course is to become comfortable in the terminal environment. IDEs will not always be available, and in a lot of applications running from the terminal is much preferred (it’s faster, and everyone has a terminal). For example, if you need to use a supercomputer to run a complex simulation you wrote, you’re gonna be running it on a Linux terminal.

This guide will however include instructions for installing an IDE called Spy- der. If you prefer to work with this that is ok, provided you can operate the terminal still. Keep in mind we will be running from the terminal in lecture examples.

We will use Python 3, don’t use a Python 2 installation as some things may not work in the future. They may still work, but better to be proactive against issues. The specific version (3.5.6 or 3.7 or 3.X etc) shouldn’t matter. Knock on wood

2 2 Windows 10

2.1 Installing Python

This is actually somewhat kind of pretty easy in Windows (most things are not when it comes to installing languages/packages that you could do super easily in Linux). Start by going here: https://www.python.org/downloads/

Click the download button for the most current version. Once the download finishes run the installer.

IMPORTANT: Don’t just automatically click through this first screen. First take note of the install location it is suggesting. Take note of this, it may or may not be needed later. Second, check the box to add Python 3.7 to PATH. This allows your terminal to know where all the stuff to run Python is. Now you can click Install now. The installation should finish quickly and you can close the installer.

3 2.2 Using Python

2.2.1 Writing Code

The application you use to write code has very few limitations. You can use something as as notepad (not the best), fully customizable text editors, Integrated Development Environments, or you can go old school and edit in terminal with or . This is really up to you.

The lab machines all have the text editor available. Install what you like on your personal machine - see suggestions below. Let’s test your Python installation in a quick and easy way. Open Notepad or Atom if you have it already and copy the following line: print("Hello, world!")

Save the file as helloWorld.py in a folder you have for this class, we’ll run it next. NOTE: If you use Notepad for this, be sure to check that it saves with the .py file extension - it might default to a text file and save it as hello.py.txt which will not work.

4 2.2.2 Using the PowerShell

Open the start menu and type powershell. Open up the Windows Pow- erShell (and maybe pin it to your start menu or taskbar). This termi- nal is much better than the windows Command Line you may have seen. Many of the commands are the same as on a Linux terminal - cd ls mkdir .... If you’re not comfortable navigating your files in the PowerShell, check out this guide https://programminghistorian.org/en/lessons/intro- to-powershell#navigation, or come get help if you still have trouble.

Navigate the PowerShell using the cd command to where you saved hello.py. If you type ls you should see it in the list of files at your current location. If it’s there then enter python hello.py

If it worked, you should see your greeting to the world. This is the stereo- typical thing to do when starting programming (See: https://www.reddit. com/r/comics/comments/64sybe/hello_world_oc/)

In this case you won’t need the file path you copied from the installer.

5 If this doesn’t work you might need to put Python in your path manually. Go to the start menu, type powershell and open the Windows PowerShell. Copy the following command, and replace with the install loca- tion you copied from the installer (get rid of the angle brackets too - NOT the quotes!).

[Environment]::SetEnvironmentVariable("Path", "$env:Path;")

For example, on my machine this looked like:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;:\Users\Daniel\AppData \Local\Programs\Python\Python37-32\")

BE CAREFUL. Messing this up could cause some serious issues on your machine. These are your computer’s environment variables, the bits of data that it uses constantly to connect things together.

Note that the Python37-32 portion should have been in the location from the installer. If not, simply add this. This name may change based on the version, so if this still doesn’t work, check the topmost Python folder and

6 choose the highest numbered folder. You can check to make sure this folder contains a python.exe file.

If you’re still having trouble, send me an email at [email protected] or come to office hours. If you need to run code ASAP look at the option to install Spyder below.

2.3 Optional: Choosing and Customizing a Text Edi- tor

Notepad++ - A barebones but still useful editor, Notepad++ handles some and an that really just remembers words you have already typed (i.e. no handy function usage info like in Atom with autocomplete packages.

Atom - This is my personal favorite, but for good reason. Atom is completely open source, meaning it is excessively customizable and many developers cre- ate add on packages for it. In it’s base form it is good too. I recommend starting by choosing a theme, and I highly recommend a dark theme - it’s eas- ier on the eyes, it’s easier to read, and the syntax highlighting shows up a bit better. (See: https://www.reddit.com/r/ProgrammerHumor/comments/ 920x95/boo/).

I recommend starting here, and once you get comfortable add some packages. Starter packages I suggest for Atom are minimap, highlight-selected, and todo-show. These packages will hopefully be ready on the lab machines. Note that there are packages in Atom for practically every language too! To install packages check the settings page.

For Python specifically: When you get comfortable programming, you may want an autocomplete or function lookup type tool. In Atom you can install autocomplete-python and Kite together to get suggestions based on yours and others’ common functions. It is also able to remember your custom functions and inputs/outputs. Note that Kite is a subscription service but has a free version.

Atom Themes - Set a theme for Atom if you want to change it up from

7 the default. Go to File →Settings →Themes. I’m a personal fan of the One Dark UI theme and the Atom Dark syntax theme. Do what you like, but remember darker is easier on the eyes (especially your ”old” TA’s eyeballs).

Other Editors: Sublime Text is another very popular and customizable editor that is widely used by . There are also other options - try Googling around for text editors for programmers or maybe even Python specifically. This is all your preference, coding should feel comfortable.

2.4 Optional: Linux Subsystem on Windows

Another thing you can do that can be quite powerful for future use or gaining experience right now is to install the Linux subsystem on Windows. This lets you run a Linux terminal on your Windows machine without having to use dual booting or a virtual machine. This is a really cool feature as you can access all of your files through the Linux terminal and run them in that environment. For example in my workflow I use Atom to write code which is saved in my regular Documents folder. Then in the Linux terminal I just navigate to it and can run it from there.

This is a good idea because it lets you gain experience with using Linux (a really really valuable skill), but also because it is so much easier to install various packages and features in Linux than on Windows. We’ll see this more later, but for now if you want to try it, go for it. Following this tutorial will get you set up: https://www.howtogeek.com/249966/how-to-install-and-use-the-linux- bash--on-windows-10/

I recommend installing Ubuntu, it is the most common and is easy to use and learn on if you’re new. Other distributions of Linux will work too if you want to try something else.

To get to your Windows files, when you open the new bash terminal cd to:

When we get to it, or if you try to do it before you will find that this has issue with running a graphical application. A handy workaround is here:

8 https://www.howtogeek.com/261575/how-to-run-graphical-linux-desktop- applications-from-windows-10s-bash-shell/

2.5 Optional: Installing and Using Spyder

Spyder is a quick and easy IDE to get started with, but as mentioned before make sure you get comfortable with running from a terminal too. Spyder is meant for use in science especially, and it has some powerful features for that specifically. You can still do everything it can from the terminal, but Spyder is a bit more accessible for those advanced features to start. It also has a handy debugging tool which you may find useful. Go here to get started: https://www.spyder-ide.org/

Click download and follow the instructions, it should be like installing any other application. The website has some usage documents if you’re looking for some Getting Started type guides. Just keep in mind that your work will be run and graded from a terminal, and you are expected to be able to use the terminal throughout the course.

2.6 Other Options

PyCharm is also a really popular IDE with some powerful features. Check it out if Spyder isn’t cutting it (I know PyCharm has better color theming for one). You can also Google for other Python IDEs or check out some forums to see what other’s workflows are like.

3 Linux (Ubuntu)

This is really easy, and it’s likely Python is already ready to use on any Linux distribution. If you have a distribution other than Ubuntu adjust the commands to match your distro. To make sure you have the most recent version of Python 3, run:

9 sudo apt-get install python3

It is important to specify the 3. Running without the 3 defaults to Python 2.7. To check this, run both of the below: python --version python3 --version

If the first shows a 2.7 version, then you will want to run python3 when you compile. For the beginning this won’t matter, and it might not ever be an issue in this class, but better to check.

Now you can run a python program! Write a file in a text editor of your choice with the line: print("Hello, world!")

Save it as hello.py somewhere you can find. Navigate to this directory in the terminal and run with: python3 hello.py

Congrats! Much easier on Linux. As with Windows, use whatever text editor you want. See the suggestions in section 2.3 if you’re looking for one.

4 Mac OSX

I don’t own a Mac to test this with but luckily it is simpler than the windows install. This website looks like it will get you in the right direction: https: //docs.python-guide.org/starting/install3/osx/

Once you can run python in the terminal then you can use your preferred text editor, Atom (mentioned in 2.3) is even available for Mac.

If there are any issues installing on Mac ask one of us and we will help you get it setup outside of class.

10 5 Running in a lab/classroom

Everything is set up! Start from Section 2.2.1. On the classroom machines there is already Atom installed. We will run through everything on the first day of class too, so if any issues come up we can address them then.

6 ALL OS - Installing Packages - VERY IM- PORTANT

DO THIS BEFORE HW 1!!

Python offers a lot of extra packages which provide tons of features once they are installed. No matter which OS you have, you will need the python package manager named pip. We will install this now, as we will need a package to run the test cases in the first homework. Note that the Windows installation should already have pip, and the Mac guide linked says pip will be installed. If you’re on Windows or Mac check that pip is installed with: pip --version

If it doesn’t work, you may have to run the installer again. If it says that this pip is for Python 2.7, then repeat the command but say pip3 rather than pip. If this also doesn’t work then this will require Googling and perhaps office hours but it should be extremely unlikely. If you’re good to go then skip over this Linux section below to install our first package in Section 6.2

6.1 On Linux

Linux distributions should also have pip ready. If the Python version check (python --version) returns a 2.7 version, then you will have to install pip3 rather than pip so it knows which library to tie it to. Check first with either pip --version

11 or pip3 --version

If you don’t get a pip version for Python 3 to come back, but your python --version check returns a version 3, you might be able to just use sudo apt-get install python-pip

If this installs and the version check on pip says it is for Python 2.7, then run the next command. If you are needing to specify python3 anyway, this is what you’ll need: sudo apt-get install python3-pip

Tada! Check the pip3 version now.

6.2 Installing a package (DO BEFORE HW 1)

Now we will need to install colorama to color code our unit tests. In your terminal (on all OSes) run pip3 install colorama

This might be already installed, but either way you should now be able to run HW01 and get started!

Note: If you end up with pip connecting to Python 3 when you check the version and these commands don’t work, then run these commands with just pip.

12