Running Python Locally

When we say running Python ‘locally’, we mean installing a version of Python (or using a version already) and executing code on your computer. There are many other ways to run Python, and one of the more convenient options is in the cloud.

Installing Python or R is fairly straightforward nowdays, but be warned: what you will see after installing them ain’t pretty. By default, Python and R code is simply the black-and-white text output of an ‘interpreter’ (something that processes lines of code by ‘interpreting’ our code into instructions to the computer, and back). People who code all the time will always install an IDE (short for: Integrated Development Environment) like RStudio or VSCode because it ‘prettifies’ the code that they run, making it easier to spot mistakes and track links between files containing code that interact.

You can complete Code Camp using just the basic Python interpreter, but you might find it easier to look at the no install options since these automatically include some basic tools for beautifying your Python code. However, if you’re sure that you want to install Python on your computer then here’s what you do…

Checking for ‘System’ Python

Many computers (especially Macs and Linux systems) will come with Python pre-installed. To find out if you have Python installed:

Open up the Terminal (under Applications > Utilities) and then type python3 --version ↩︎.

Open the Command Prompt (either run cmd.exe or search for Command Prompt from the Windows menu) and then type python3 --version ↩︎.

Tip

↩︎ means hit Enter or Return on your keyboard. Don’t try to paste the symbol into the Terminal or Command Prompt.

In both cases, if Python is already installed then you will see something like the following (your exact version may differ):

Python 3.8.16

If you see this kind output then, congratulations, you have Python installed! If you’re unsure what all this was about watch the video below.

Note

Depending on how Python was installed, you may find that typing just python produces an error, while typing python3 is successful.

Action

Just to give you a sense of how to interact with Python from the command line, here’s a short video from a Mac showing how to start/stop Python and execute code to print out the result of 2+2. It’s just a sanity-check:

Versions

Don’t worry too much about what version of Python you see listed (in the example above it’s 3.8.16) as long as you have at least verion 3.8.0. You should not use Python2 (e.g. 2.7.6) or versions of Python3 earlier than 3.6.

Installing ‘Reference’ Python

If you don’t have Python installed, then the ‘reference’ version of Python is the one to use since it’s produced by the Python Software Foundation and is required by QGIS anyway. Whatever operating system you’re using, you should start from the downloads page and follow the instructions from there. You will want a Stable Release, not a Pre-release version of Python.

Once it’s installed, you just use the Terminal or Command Prompt as above in Action.

Our Recommendation

You would need to have ‘Reference’ Python installed anyway to make the most of QGIS, so we’d use that one. And getting used to the Terminal/Command Prompt is no bad thing since you’ll be using it a lot in the future.

Running Code Camp

This short video shows how to copy code from the Code Camp micro-site over to VSCode (so that you can save your work), and from there into the Python ‘interpreter’ (what you have just installed):

You can copy and paste multiple lines of code into the interpreter.