Software Exercise 2.3: Using Python, Pre-Built¶
In this exercise, you will install Python, package your installation, and then use it to run jobs. It should take about 20 minutes.
We chose Python as the language for this example because: a) it is a common language used for scientific computing and b) it has a straightforward installation process and is fairly portable.
Running any Python script requires an installation of the Python interpreter. The Python interpreter is what we're using when we type
python at the command line. In order to run Python jobs on a distributed system, you will need to install the Python interpreter (what we often refer to as just "installing Python"), within the job, then run your Python script.
There are two installation approaches. The approach we will cover in this exercise is that of "pre-building" the installation. We will install Python to a specific directory, and then create a tarball of that installation directory. We can then use our tarball within jobs to run Python scripts.
Interactive Job for Pre-Building¶
The first step in our job process is building a Python installation that we can package up.
- Create a directory for this exercise on
Download the Python source code from https://www.python.org/.
[email protected] $ wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
Of our options - submit server, interactive job, personal computer - which should we use for this installation/packaging process? Once you have a guess, move to the next step.
Due to the number of people on our submit server, we shouldn't use the submit server. Your own computer probably doesn't have the right operating system. The best place to install will be an interactive job. For this job, we can use the same interactive submit file as Exercise 2.2, with one change. What is it?
Make a copy of the interactive submit file from Exercise 2.2 and change the
transfer_input_filesline to the Python tarball you just downloaded. Then submit it using the
[email protected] $ condor_submit -i build.submit
Once the interactive job begins, we can start our installation process. First, we have to determine how to install Python to a specific location in our working directory.
- Untar the Python source tarball and look at the
README.rstfile in the
Python-3.7.0directory. You'll want to look for the "Build Instructions" header. What will the main installation steps be? What command is required for the final installation? Once you've tried to answer these questions, move to the next step.
There are some basic installation instructions near the top of the
README. Based on that short introduction, we can see the main steps of installation will be:
./configure make make test sudo make install
This three-stage process (configure, make, make install) is a common way to install many software packages. The default installation location for Python requires
sudo(administrative privileges) to install. However, we'd like to install to a specific location in the working directory so that we can compress that installation directory into a tarball.
You can often use an option called
configurescript to change the default installation directory. Let's see if the Python
configurescript has this option by using the "help" option (as suggested in the
[email protected] $ ./configure --help
Sure enough, there's a list of all the different options that can be passed to the
configurescript, which includes
--prefix. (To see the
--prefixoption, you may need to scroll towards the top of the output.) Therefore, we can use the
$PWDcommand in order to set the path correctly to a custom installation directory.
- Untar the Python source tarball and look at the
Now let's actually install Python!
From the job's main working directory, create a directory to hold the installation.
Move into the
Python-3.7.0directory and run the installation commands. These may take a few minutes each.
[email protected] $ cd Python-3.7.0 [email protected] $ ./configure --prefix=$PWD/../python [email protected] $ make [email protected] $ make install
The installation instructions in the
README.rstfile have a
make teststep between the
make installsteps. As this step isn't strictly necessary (and takes a long time), it's been omitted above.
If I move back to the main job working directory, and look in the
pythonsubdirectory, I should see a Python installation.
I have successfully created a self-contained Python installation. Now it just needs to be tarred up!
[email protected] $ tar -czf prebuilt_python.tar.gz python/
Before exiting, we might want to know how we installed Python for later reference. Enter the following commands to save our history to a file:
[email protected] $ history > python_install.txt
Exit the interactive job.
[email protected] $ exit
Create a script with the following lines called
import sys import os if len(sys.argv) != 2: print('Usage: %s MAXIMUM' % (os.path.basename(sys.argv))) sys.exit(1) maximum = int(sys.argv) n1 = n2 = 1 while n2 <= maximum: n1, n2 = n2, n1 + n2 print('The greatest Fibonacci number up to %d is %d' % (maximum, n1))
What command line arguments does this script take? Try running it on the submit server.
We now have our Python installation and our Python script - we just need to write a wrapper script to run them.
- What steps do you think the wrapper script needs to perform? Create a file called
run_fib.shand write them out in plain English before moving to the next step.
- Our script will need to
- untar our
- access the
pythoncommand from our installation to run our
- untar our
- Try turning your plain English steps into commands that the computer can run.
run_fib.shscript should look something like this:
#!/bin/bash tar -xzf prebuilt_python.tar.gz python/bin/python3 fib.py 90
#!/bin/bash tar -xzf prebuilt_python.tar.gz export PATH=$(pwd)/python/bin:$PATH python3 fib.py 90
Make sure your
run_fib.shscript is executable.
Make a copy of a previous submit file in your local directory (the OpenBugs submit file could be a good starting point). What changes need to be made to run this Python job?
Modify your submit file, then make sure you've included the key lines below:
executable = run_fib.sh transfer_input_files = fib.py, prebuilt_python.tar.gz
Submit the job using
.outfile to see if the job completed.