python installation and usage guide -...

12
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: Linux Subsystem on Windows ............ 8 2.5 Optional: Installing and Using Spyder ............. 9 2.6 Other Options ........................... 9 3 Linux (Ubuntu) 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

Upload: others

Post on 01-Sep-2019

53 views

Category:

Documents


0 download

TRANSCRIPT

Python Installation and Usage Guide

August 20, 2018

Contents

1 Overview 2

2 Windows 10 32.1 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.2.1 Writing Code . . . . . . . . . . . . . . . . . . . . . . . 42.2.2 Using the PowerShell . . . . . . . . . . . . . . . . . . . 5

2.3 Optional: Choosing and Customizing a Text Editor . . . . . . 72.4 Optional: Linux Subsystem on Windows . . . . . . . . . . . . 82.5 Optional: Installing and Using Spyder . . . . . . . . . . . . . 92.6 Other Options . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Linux (Ubuntu) 9

4 Mac OSX 10

5 Running in a lab/classroom 11

6 ALL OS - Installing Packages - VERY IMPORTANT 116.1 On Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116.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 shouldhave 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 optionaldetails are included in the Windows section which you may find fit your workflow or are easier to use. (The text editor section actually can apply to allOSes).

We want everyone to build comfort with using a terminal. Whilethere are lots of IDEs (pretty and easy to use development environments)out there, a goal of this course is to become comfortable in the terminalenvironment. IDEs will not always be available, and in a lot of applicationsrunning from the terminal is much preferred (it’s faster, and everyone has aterminal). For example, if you need to use a supercomputer to run a complexsimulation 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 theterminal still. Keep in mind we will be running from the terminal in lectureexamples.

We will use Python 3, don’t use a Python 2 installation as somethings may not work in the future. They may still work, but betterto 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 arenot when it comes to installing languages/packages that you could do supereasily in Linux). Start by going here:

https://www.python.org/downloads/

Click the download button for the most current version. Once the downloadfinishes 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, itmay or may not be needed later. Second, check the box to add Python 3.7 toPATH. This allows your terminal to know where all the stuff to run Pythonis. Now you can click Install now. The installation should finish quickly andyou 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 usesomething as basic as notepad (not the best), fully customizable text editors,Integrated Development Environments, or you can go old school and edit interminal with VIM or eMacs. This is really up to you.

The lab machines all have the text editor Atom available. Install what youlike on your personal machine - see suggestions below. Let’s test your Pythoninstallation in a quick and easy way. Open Notepad or Atom if you have italready and copy the following line:

print("Hello, world!")

Save the file as helloWorld.py in a folder you have for this class, we’ll runit next. NOTE: If you use Notepad for this, be sure to check that it saveswith the .py file extension - it might default to a text file and save it ashello.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, checkout 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 <LOCATION> with the install loca-tion you copied from the installer (get rid of the angle brackets too - NOTthe quotes!).

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

For example, on my machine this looked like:

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

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

Note that the Python37-32 portion should have been in the location fromthe installer. If not, simply add this. This name may change based on theversion, 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 foldercontains a python.exe file.

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

2.3 Optional: Choosing and Customizing a Text Edi-tor

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

Atom - This is my personal favorite, but for good reason. Atom is completelyopen 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 recommendstarting 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 bitbetter. (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, andtodo-show. These packages will hopefully be ready on the lab machines.Note that there are packages in Atom for practically every language too! Toinstall packages check the settings page.

For Python specifically: When you get comfortable programming, youmay want an autocomplete or function lookup type tool. In Atom you caninstall autocomplete-python and Kite together to get suggestions based onyours and others’ common functions. It is also able to remember your customfunctions and inputs/outputs. Note that Kite is a subscription service buthas 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, butremember darker is easier on the eyes (especially your ”old” TA’s eyeballs).

Other Editors: Sublime Text is another very popular and customizableeditor that is widely used by programmers. There are also other options -try Googling around for text editors for programmers or maybe even Pythonspecifically. 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 gainingexperience right now is to install the Linux subsystem on Windows. This letsyou run a Linux terminal on your Windows machine without having to usedual booting or a virtual machine. This is a really cool feature as you canaccess all of your files through the Linux terminal and run them in thatenvironment. For example in my workflow I use Atom to write code whichis saved in my regular Documents folder. Then in the Linux terminal I justnavigate to it and can run it from there.

This is a good idea because it lets you gain experience with using Linux (areally really valuable skill), but also because it is so much easier to installvarious packages and features in Linux than on Windows. We’ll see this morelater, but for now if you want to try it, go for it. Following this tutorial willget you set up:

https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-

bash-shell-on-windows-10/

I recommend installing Ubuntu, it is the most common and is easy to useand learn on if you’re new. Other distributions of Linux will work too if youwant 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 hasissue 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 beforemake sure you get comfortable with running from a terminal too. Spyder ismeant for use in science especially, and it has some powerful features for thatspecifically. You can still do everything it can from the terminal, but Spyderis a bit more accessible for those advanced features to start. It also has ahandy 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 anyother application. The website has some usage documents if you’re lookingfor some Getting Started type guides. Just keep in mind that your work willbe run and graded from a terminal, and you are expected to be able to usethe terminal throughout the course.

2.6 Other Options

PyCharm is also a really popular IDE with some powerful features. Check itout if Spyder isn’t cutting it (I know PyCharm has better color theming forone). You can also Google for other Python IDEs or check out some forumsto 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 anyLinux distribution. If you have a distribution other than Ubuntu adjust thecommands to match your distro. To make sure you have the most recentversion of Python 3, run:

9

sudo apt-get install python3

It is important to specify the 3. Running without the 3 defaults to Python2.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 youcompile. For the beginning this won’t matter, and it might not ever be anissue in this class, but better to check.

Now you can run a python program! Write a file in a text editor of yourchoice with the line:

print("Hello, world!")

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

python3 hello.py

Congrats! Much easier on Linux. As with Windows, use whatever text editoryou 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 windowsinstall. 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 preferredtext 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 youget 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 machinesthere is already Atom installed. We will run through everything on the firstday 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 oncethey are installed. No matter which OS you have, you will need the pythonpackage manager named pip. We will install this now, as we will need apackage to run the test cases in the first homework. Note that the Windowsinstallation should already have pip, and the Mac guide linked says pip willbe 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 thatthis pip is for Python 2.7, then repeat the command but say pip3 rather thanpip. If this also doesn’t work then this will require Googling and perhapsoffice hours but it should be extremely unlikely. If you’re good to go thenskip 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 pip3rather 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, thenrun the next command. If you are needing to specify python3 anyway, thisis 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 yourterminal (on all OSes) run

pip3 install colorama

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

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

12