Skip main navigation

How do I run Python code?

This article briefly describes some of the main ways you can create, edit and run Python code.

Python

Before learning more programming or coding skills next week it’s worth taking some time to look at the different pieces of software you might use to run code on your computer. Depending on the context this might be at a command line in a terminal window, in a fully featured application known as an Integrated Development Environment, or even from a web browser. The purpose of this article is to highlight a few of these possibilities and to point you in the direction of pieces of software that will help you learn to code using Python – a popular language for data analysis.

Coding in Python allows us to automate more complex tasks, and even, when more experienced, write your own image analysis approaches.

Installing Python

We’ll be using Python, and so the first and most obvious question to ask is ‘Does my machine already have Python?’. If you have a Linux or Mac operating system, the chances are the answer to this question is Yes, but if you are running Windows you may need to download and install a version of Python.

It is possible to download and install a stand-alone version of Python but, for Windows especially, it’s easy to run into configuration problems. For most beginners, we recommend downloading Anaconda (see Anaconda link below this article) instead. This is available for Windows, Mac, and Linux and as well as installing the Python language itself it also includes several useful tools to help you write code and manage your Python packages (we’ll come back to packages/libraries later; they add commonly-used functions to your code).

Python through the Command Line

Once you have Python installed, the most direct way to start testing it out is through a so-called Python ‘shell’ in a command line Window. As mentioned above, if you are using Linux or Mac you may just be able to open a Terminal or Command Line from the start menu or Launchpad and type ‘python’ at the prompt (or ‘python3’ on the most recent versions of macOS).

For Windows it can be a little more complicated to get Python working through the command line, but if you have installed Anaconda you can just open ‘Anaconda prompt’ through the Start Menu, then just type ‘python’ at the prompt as shown here:

Accessing the Anaconda prompt via the start menu in Windows, with the command prompt shown along side.

Once you are in a Python shell you can type in Python commands and the computer will follow the instructions (provided they follow the rules of Python and make sense). There’s more on programming Python in later videos and other articles, but there’s a few lines of simple code shown below for you to try out in a shell of your own.


a = 10
b = 2.3
a+b
a/b
a*b

print("Hello world!")

Text Editors

It is possible to write your code direct to the shell or interpreter, but it’s difficult to write more complex programs without making errors. Instead, it’s far more common to write out your entire program in either a single or, possibly, multiple text files before running it at the command line. There are numerous text editor programs available in which to write text files, each of which has its own set of options and features available to help you.

Windows has it’s own simple text editor with limited functionality, Notepad, which many of you will have used previously. Other text editors that are free to download include Atom, Notepad++ and Geany. These have customisable features such as syntax highlighting (the colouring or styling of code based on identification of keywords or data types), code navigation (helping you find particular parts of large files quickly), autocompletion (similar to predictive text – saving time typing out long variable names), and many others.

All the text editors mentioned so far are separate programs with their own graphical user interfaces. It’s also possible to edit and create text files entirely within a terminal window using a command line text editor such as Vim, Emacs or Nano, but we wouldn’t necessarily recommend these for beginners . We list a few freely downloadable text editors in the table below.

Editor Type
Spyder IDE (see below)
Notepad++ Text GUI
Geany Text GUI
Atom Text GUI
Vim Command line
Emacs Command line
Nano Command line

Running a Python text file

Whatever text you decide to use, the process of running it is the same. You save it in a directory of your choice with the file name of your choice, but with the file extension ‘.py’. For example, you might save a file named ‘my_program.py’ in your Documents folder. Then you need to open a terminal window, navigate to the directory where you saved the file (see the article on command line basics), then type ‘python’ followed by the name of the file. In the example show belown we have run the program ‘my_program.py’, which just prints out a short message then ends.

The Anaconda prompt having run the program 'my_program.py' and displayed the text 'Hello Wordl!' to the screen.

Integrated Development Environments: IDEs

Many programming tasks, even relatively complicated ones, can be achieved using just a text editor and a command line. However, many programmers choose to go a step further and use an Integrated Development Environment (IDE) program. These can include an enormous range of features, in addition to the features common in text editors such as syntax highlighting, but generally the feature that sets them apart from ordinary text editors is the ability to run, test, and debug code within the IDE itself.

Some IDEs are specifically for software development on certain platforms such as Visual Studio for Windows or XCode for Mac, while others such as NetBeans and Eclipse are available for multiple platforms and support development in multiple programming languages. However, for the purposes of this course, the only IDE we recommend you look at is Spyder, an IDE specifically developed for scientific programming using Python. Conveniently, Spyder is bundled with Anaconda so if you have downloaded and installed Anaconda you should already have a copy of Spyder ready to run.

We will see more details on using Spyder later.

Jupyter Notebooks

One final method of running code that we use in this course, and you are likely to encounter elsewhere, is using Python Notebooks. These are opened and usually written using an ordinary web browser window and contain a mixture of text, code and chunks of code called cells, which you can run individually or sequentially. Generally, these are used for writing reports on a piece of scientific work while integrating any Python code used into the document itself, a bit like a live lab book.

Commonly, though they appear in your web browser, Jupyter Notebooks usually requires the installation of both Python and the Jupyter software itself on your machine. The code written in them appears in your browser window but is in fact run using the Python installation on your local machine, possibly using data stored there. You won’t need to install Jupyter locally for this course, but for more details visit the link below.

A second type of Jupyter Notebook which we will use for this course is Colab Notebooks, developed by Google (see link below). This is a Jupyter Notebook environment in which you can run Python code, and write your own descriptive text, but instead of running locally on your machine, any code you write makes use of cloud computing services, and the notebooks and any data you may use and save are stored remotely using Google Drive.

This distinction between local and cloud computing is particularly important when undertaking more complex machine learning tasks, specifically deep machine learning, with Colab able to draw on significantly more computing power than most desktop setups. In particular, Colab allows for the use of GPU acceleration which can greatly speed up deep learning tasks. The downside to this is that is currently a free service (with limited usage, and optional paid upgrades), so the computational resources are not guaranteed and you might have to wait to run your code. To make and run Colab notebooks you will need to use a free Google account (it’s possible you already have this through a Gmail or Google Drive account).

Summary

As well as a programming language, Python is also itself a program, which can be accessed in a number of ways, including:

  • A command line in a ‘shell’ in a terminal window
  • A text file written in a text editor
  • An IDE such as Spyder
  • A Jupyter Notebook
  • A Colab notebook utilising cloud computing resources

Don’t worry if you’re still unsure about how all these methods work. The aim of the article is just to signpost all the different contexts in which you might use and encounter Python code throughout the course. There’ll be more detail as and when we need to use them as we progress.

This article is from the free online

Introduction to Image Analysis for Plant Phenotyping

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education