An Introduction to Python
Python is a programming language, which is to say, it’s a way to write down instructions for computers to do things so we don’t have to. There are tons of programming languages out there and as you get more and more comfortable with programming, you’ll undoubtedly find yourself working with more than one of them. The fact that there are so many languages out there also means that we should probably justify why we opt to use python over any of the other choices, and there are a few big reasons:
- Python is nice: It works hard to hide unnecessary details about how computers work (people often say it’s a high-level language)
- Python is flexible: It’s not the fastest language, but it can do just about anything. As scientists, we generally need flexibility more than speed.
- Python is popular: As opposed to something like Mathematica, another nice, flexible language, Python has a huge user base. This means most Python questions can be resolved just with a bit of googling.
Note: If you start googling around, one of the first things you’ll figure out about python is that there are two main versions of it. There’s Python 2, which first came out in 2000, and python3 which came out in 2008. Migration to Python 3 has been incredibly slow, so you’ll probably see stuff talking about Python 2 when you try to look stuff up on the internet. Despite that, we really want to be using Python 3. The features are worth it, and Python 2 is no longer formally supported.
What is Anaconda?
Unlike a regular application, e.g. Slack, lots of different organizations have their own distributions of Python which all have slightly different use cases. For the most part, we’re agnostic as to what distribution you use, but if you’re just getting started, we’ll recommend Anaconda.
Anaconda is a distribution of Python that comes with various scientific computing packages (NumPy / SciPy / matplotlib/etc.) ) preinstalled.
It also provides extra tools, like its own installer/package manager (conda
).
As a group, we have moved over to using Anaconda on the group computers. All the supercomputers we use (Hyak/Mox & NeRSC) have Anaconda installed or available as well.
Using Anaconda on the Group Computers
If you are working on one of the group computers, they all already have anaconda3 installed on them. The first time you use Anaconda on one of the computers, you’ll need to do a bit of set up work. Every other time it should “just work”.
To start, you’ll need to ssh
onto one of the group computers.
Once you’ve got that working, run
$ conda -V
if Anaconda has been set up, you should get something like
$ conda -V
conda 4.8.3
if not we’ll need to set things up.
Add the following to your ~/.bashrc
file
export PATH=/opt/anaconda3/bin:$PATH
Now, exit and log in again.
Here are some steps to do the same with a text editing software called emacs
.
Log on to one of the group computers. If you don’t know how, this will help you out.
When you do, you should see something that looks like
[yourusername@thecomputer ~]$
Once you have this, we’ll use emacs
to edit the .bashrc
file in our home directory, by doing
[yourusername@thecomputer some_folder] emacs -nw ~/.bashrc$
You will open up the file at that point. Use the arrow keys to go to the bottom of the file, and add the line
export PATH=/opt/anaconda3/bin:$PATH
Once you do this, hit Ctrl+x
and then Ctrl+c
and then type y
.
This will save the changes you made and exit the emacs software.
When you log off and log back in, Anaconda should be ready to go.
If this doesn’t work for some reason, consult our shared drive ComputerSOP documentation or a senior member of the group.
Installing Anaconda3 on your laptop or desktop at home
Go to the Anaconda downloads page and download the appropriate installer. Almost certainly, you’ll want the 64-bit Graphical Installer.
Follow the installer’s instructions and recommended installation instructions.
You may be asked if you want to download PyCharm
along the way, which is the group’s recommended Integrated Development Environment (IDE) for coding and debugging in Python.
If you have a Mac, you can add
export PATH=/opt/anaconda3/bin:$PATH
to your ~/.bashrc
as shown above to use anaconda on the command line when you type python
.
If you have a PC, you can use the Anaconda Powershell
to access anaconda on the command line, or you can download Windows Susbsystem for Linux and do the Mac-like installation of it in your subsystem.
Installing Python 3
If for some reason you prefer to use the standard distribution Python over the Anaconda one (this is discouraged), that works too. To do this, go to the Python Organization downloads page and download whichever version they’re saying is the latest (as of writing this that’s currently 3.8). That should download an installer and we’ll just do what the python.org people tell us to do to get it installed.
Next:
What is an IDE?
Previous:
Getting Started
Got questions? Ask them on the McCoy Group Stack Overflow