Installing Python
In this guide, the 'third version' of Python, Python 3 will be referred to as Python. You may come across its previous version, Python 2. At face value, there are minor differences between the two, but all of the software packages we will work with run on Python 3 not 2, so take note.
There are several ways of installing Python. Your system may already have it installed by default. Python is the base language for lots of things, but additional packages must also be installed to provide access to fast calculations, machine learning and more. These additional packages are dependencies, which must be managed so that Python scripts can be run reproducibly. To make sure we are all on the same page, this
Before you go further, make sure your terminal is setup and you are familiar with using your command line.
Downloads
This section runs through the steps to download and install miniconda on your system. Miniconda helps you to manage your installed packages and Python versions, which is important in maintaining reproducible programs.
Visit the miniconda download page and download miniconda for your system and follow the installation guide.
Set Up A Virtual Environment
From now on, you will use Python from the command line in virtual environments managed by miniconda. The next step is to set up a virtual environment in which to run your programs.
Run Python
First, activate your virtual environment.
Running the Python Interpreter
You can run Python by simply typing python
, which starts the Python
interpreter. In this mode, you can type Python code into the command line, line
by line, and it will be executed, for example:
>>> python
Python 3.12.1 | packaged by Anaconda, Inc. | (main, Jan 19 2024, 09:52:31) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
>>> quit()
The last line (quit()
) exits the interpreter and returns to the normal command
line.
Running Python Scripts
First, create a file containing Python code. You can either open a new text
file, write print("Hello World")
into it, and save it in you current working
directory as test.py
, or run echo print("Hello World") > test.py
. You can
then run the file:
python test.py
Which should print "Hello World" in the terminal.
Running Jupyter Notebooks
Jupyter notebooks are a popular way of running code in an interactive way. If
you have Jupyter Notebook installed,
you can start it with python -m notebook
, and then go to the address it prints
out in your browser.