Python Installation and Setup for Computing#

Over the course of these notebooks, we’ll be discussing everything you nee to know to get up and running with modern scientific research in astronomy. We’ll cover

  • UNIX environments and how to install python and associated packages in conda enviornments (and why)

  • Basic python and writing reproducible code

  • Maintaining and versioning you work with Github

Prerequisites#

Before diving into the notebook, there are a few things you should set up before hand. These vary somewhat by operating system.

MacOS#

To set up a Mac computer for scientific programming, you’ll want to install the following:

  1. X-code developer command line tools. The easiest way to get this is to install the next tool, which will prompt this to install during the process.

  2. Homebrew. This is a software manager that has several packages you’ll need on your system. You can check if you already have these installed by opening the Terminal app and typing brew. If the command is not found, you’ll need to install it. To do so, go to this site and copy the link, which will look like

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

You can run this in your open terminal to install brew and x-code cli tools.

If you have an Intel chip mac, this step is now done, otherwise, on M1+ macs, you’ll need to add the location of the brew files to your PATH.

Note

Your PATH is a set of folders on your computer that the shell/terminal knows to look in when searching for executable code to run. When you type a program name into your shell (like brew), the files associated with running brew must be in your PATH in order to be found and run. Most of the time, software we install will automatically add itself to our path, or will install in a location that is in our PATH. So we normally don’t have to worry about this.

To add the brew install path to your PATH, run the following two lines in your shell:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Tip

Here, we are using shell commands: echo (which just prints the string after it) and >>, which writes that to a file provided after, which in this case is your shell’s profile, stored in your home directory (which ~ is shorthand for). We’ll talk more about profiles later.

After this, you should be able to restart your terminal, and when you type brew, it should not warn about the command not being found. As a note, this guide assumes you are using a new enough computer that zshell is your default shell, rather than something like bash. Talk to me if you run into issues.

  1. Conda. Now that we have brew and CLI tools, we need to install python, with a package manager. There are several out there, but in the astrophysics community, the overwhelming majority use the miniconda distribution to manage python and python environments. We’ll do that here as well, as that will then translate to other software installation guides you come across. To install it, head over to the Miniconda Website and choose the installer for your operating system.

Warning

In the past, I have found that installing via the pkg installer places files in a different, more annoying location. I recommend instead downloading the bash version for your OS/computer type. Then, you can use the shell cd command to, e.g., cd ~/Downloads, then run, e.g.,

bash Miniconda3-py310_23.3.1-0-MacOSX-x86_64.sh

and follow the instructions. I recommend accepting all of the suggested locations and defaults.

Great! We now have a working set of command line tools, and conda to help us manage python and its environments. We’ll cover that in the next section.

Windows#

Windows itself is not particularly conducive to scientific computing, and you will struggle trying to use a Windows OS for research purposes. Luckily, Windows now supports something called WSL, or Windows Subsystem Linux, which allows you to install a linux distribution directly on your Windows machine. When using WSL, you will essentially be using Linux, for which most scientific programs are written, and will be able to mirror all the conda environment and coding paths needed for your research.

To get it installed, simply open a Windows Powershell and type:

wsl --install

which will install the latest distribution of Ubuntu, which is fine to use as a linux distribution for our purposes.

Once you have installed WSL, you will need to create a user account and password for your newly installed Linux distribution — it is basically like a second mini operating system, with its own username/password and filesystem. Follow the instructions and select a username and password (these need not be the same as for your PC).

Note

When you are setting (or entering) passwords in terminals, here included, generally nothing shows up as you type. This is for security, just be careful while typing and hit enter to submit.

Once you have wsl installed, you should have a program on your computer which is an Ubuntu Shell. When you open it, you’ll have a terminal, but this is not a windows powershell, it is in fact UNIX-like and is accessing your new WSL distribution. It is recommended to start out by upgrading all default installed packages via

sudo apt update && sudo apt upgrade

After that, you’re ready to install miniconda, which is the python + package distribution manager we’ll be using.

There are several out there, but in the astrophysics community, the overwhelming majority use the miniconda distribution to manage python and python environments. We’ll do that here as well, as that will then translate to other software installation guides you come across. To install it, open up your Ubuntu shell and type

curl -sL "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh" > "Miniconda3.sh"

Followed by

bash Miniconda3.sh

I would accept all the default suggestions.