Module: Using Python
Learning Goals
After completing this module, the student should:
- Be able to run a simple script from a variety of contexts.
- Understand the difference between these contexts.
Learning Outcomes
In terms of specific knowledge, skills, and abilities, after completing this module students should be able to:
- Be able to run a simple Python script from the command line.
- Be able to run simple lines of code from Python’s REPL.
- Be able to run a simple Python script from a Jupyter notebook in Jupyter Lab.
Readings
Acitivity 1: Hello, World!
In this activity, you will run Python from the command line and become acquainted with its most basic mode of operation.
To get started, log onto the Rivanna shell and move into in the course directory you created for this class.
Which Python?
Once you are in Rivanna, you will want to know which version of Python you have installed as the default.
To find out, from the command line type:
which python
This will show you the “path” to a python interpreter. For example:
If you do not see “anaconda” in that path, then run the following command:
module load anaconda
This will update your environment to use Anaconda’s distribution of Python.
After running the command, enter the command to see which pythion is being used to confirm that it is the one from Ancaconda.
Aside
Note that you can use module
to run other programs on your Rivanna account.
Type module list
to see which programs have already been installed on your account.
Type module spider
to see all the programs you can install.
The Python Interactive Shell
From the command line, enter python
You should get the Python Shell:
This is also called the Python standard REPL, which stands for “Read-Eval-Print Loop”.
Make sure you see that you are using version 3 of Python.
If you see Python 2, exit the shell by entering quit()
and try again by entering python3 at the command line.
At the >>>
prompt type print("Hello, World!")
and press return.
If you’ve never used Python, you’ve just completed an important ritual. If you have used Python, well, you did it again :-)
Try this
Now, enter following line at the prompt and press return:
import this
What do you see?
To exit the Python Shell, enter quit()
or exit()
and hit return.
Running Python Files
Now create a file called hello.py
using the command line editor nano
. Enter the same commands you used above.
Then run it from the command line by directly invoking the Python interpreter python.
Activity 2: Using Jupyter Lab
Now that we have run Python on Rivanna from the command line, let’s try it using a Jupyter Notebook.
Go the OnDemand site to access Rivanna. As a reminder, the URL is https://rivanna-portal.hpc.virginia.edu/.
From the Interactive Apps menu, select JupyterLab and fill out the form to initiate a new session. Your form should have the following values:
Note that you may increase the number of hours, cores, and megabytes of RAM, but asking for too much will increase the time it takes to start your session. So select just the resources needed and enter the course allocation given by your instructor if this value is different than in the image above.
Once the session is ready, launch the notebook.
Once you are in the notebook, use file system tab on the left to get to the directory of your personal assessments repo. Remember, you created two repos for this class — one for course content from the instructor, and one for your own course work. Use the latter for this exercise.
In a code cell in the notebook, enter the code to print "Hello, World!"
, and run the cell.
Save your notebook as hello-world.ipynb
.
Quiz
Q1. Which of the following are valid ways to run lines of Python code?
A. From within the GitHub website, by clicking on a Jupyter notebook.
B. From the command line, by typing in python, pressing return, and then interactively entering the lines.
C. From within a code cell in a Jupyter notebook running on Jupyter Lab.
D. From a text file, with a .py extension. as an argument to calling Python from the command line.