Python – Exercises

Part 1. Python shell and Python script

1.1 Python shell vs Bash shell

A "shell" is a program that takes commands from the keyboard and gives them to the operating system to perform. Bash is the default "shell" in most Linux systems.

#In a Bash shell, you can run bash command like "ls"
ls -al

#Switch from Bash to Python shell

#Run python commands
import os

#Exit Python shell and back to Bash


1.2. Check versions of Python and Python packages in either Bash shell or Python shell

#In a Bash shell, check versions for Python and Python package Numpy
python -V

pip show numpy

#Switch from Bash to Python shell

#In Python shell, check versions for Python and Python package Numpy
import sys

import numpy

#exit python shell

Python shell can be a powerful tool for debugging Python environment errors.


1.3 Working with Python script.

In the Linux workshop, you learned how to write a shell script ("Bash"). Just like a (Bash) shell script, which is a file of a set of "Bash" commands, a "Python" script is a file with a set of "Python" commands.

In this exercise, you will create a python script file named "" with the following lines. You can use any text editor to create this file, for example, vi, nano, Notepad++ on Windows, BBEdit on Mac, et al.


import numpy

print("Hellow World")

If you have problem to use any of the Text editors, you can simply copy a pre-made script file to your current directory:

cp /shared_data/qisun/ ./ 


Run the script

#If you run script this way, you would get an error message "Permission denied". Try it.

#However, the following command would work. Why? The answer is in the text right after this block of code. 
python ./

#Change the file to an executable file. Do you see the difference before and after you run "chmod a+x"?
ls -l

chmod a+x

ls -l

#now try again

#Can I run the script without the "./" part? Try it.

#if you include the current directory in the $PATH variable, now you can
export PATH=./:$PATH

The Linux shell command "ls -l" tells you whether a script file is readable or executable. If a script file is readable, you can run the script with the command "python". In this case, the Python interpreter is the executable which read the content of the script file. However, if a script is executable and the file starts with a shebang line, you can run the script with the command "./". The "./" part is needed here, because it specifies the path of the script file. You can skip "./" if you include the current directory in the Linux $PATH variable.


Part 2. Install Python software with PIP

2.1 Check the version of python and pip

On BioHPC computers, the "python" command points to "python3.6.7", with "pip" command linked to this default python. The "python2" command points to "python2.7.15", with "pip2" command linked to python2.

Before you install any python modules, it is always a good idea to ask these three questions.

  1. Which copy of Python installation will you be running?
  2. What is the version of the Python?
  3. Whether the pip command you are running is associated with the Python you will be running?

Use the following commands to address these questions:

which python 
ls -l /usr/local/bin/python
python -V         ##use  capital V
which pip
head -n1 /usr/local/bin/pip     # the "head -n1" command prints the first line of a text file


2.2 Switching default python using "module" command.

To execute python 3.6, you can use the alias "python" as the command or full path command "/usr/bin/python3.6". Linux "module" function provides an easy way to switch default "python".

# list the available modules
module avail

# switch default python/pip to version 3.9.6
module load python/3.9.6
which python
which pip

# switch back to default python
module unload python/3.9.6
which python
which pip


2.3 Install and use python module python-dummy Install a dummy package called "python-dummy" in the directory ~/.local/

pip install python-dummy --user

Check the directory ~/.local/ to find the newly installed package, you should see the installation directory "python_dummy-0.1.0.dist-info" and a file ""

cd /home/qisun/.local/lib/python3.6/site-packages/
ls -lrt


2.4 Check the "pip-install-test" file in Python shell.


import dummy

After you are done, press “Ctrl-d” or type "quit()" to exit python shell.

• Make sure that you type double-underline for the part "__file__" .


Part 3. Install PYTHON software with Conda

3.1 If you do not already have Miniconda3 in your home directory, install it now. If you are not sure, run the command "ls -l $HOME". If you do not see a directory called "miniconda3", you need to install it.


chmod u+x 


3.2 Install/run pysam in Conda base. In this exercise.

source $HOME/miniconda3/bin/activate

which python

python -V 

conda install -c bioconda pysam

conda install -y pip

which pip

head -n1 ~/miniconda3/bin/pip


3.3 Install/run pysam in a Conda environment

Create a virtual environment and give it a name “pysam”, install pysam the virtual environment. This time, you will use "mamba", an alternative Conda package manager.

conda install mamba

mamba create -c bioconda -n pysam pysam


Start the pysam environment, and run python

conda activate pysam
which python
python -V
which pip

Run "conda activate pysam" if you are already in Conda "base". Otherwise you can use a single command "source $HOME/miniconda3/bin/activate pysam" to get into the environment directly.


3.4 Create a conda environment with a different version of Python Sometimes, you need to run an old script that requires python 2.7. You can create a conda environment with python =2.7

mamba create -n myNewPipeline python=2.7
mamba activate myNewPipeline
which python
which pip
python -V  

Note in the first command there is no package name. This step would create an empty python2.7 environment, within which you can use pip to install other python modules.

When you run "conda" or "mamba" to install packages, you will be prompted to inspect the versions to be installed, and answer "y" to continue. If you wish to skip this step, use the "-y" option: "conda install -y".


3.5 Exit conda

You might need to run this command twice to exit Conda environment and Conda base.

conda deactivate


Part 4. Jupyter Notebook

Pick a number between 8009 and 8039 as the port you will use, for example 8029. Verify whether this port is taken or not. If you do not see "8019 ... LISTEN" with the following command, that means the port is available.

netstat -tulpn | grep 8029


Run following command to start Jupyter notebook daemon.


export PYTHONPATH=/programs/jupyter3/lib/python3.6/site-packages:/programs/jupyter3/lib64/python3.6/site-packages

export PATH=/programs/jupyter3/bin:$PATH

jupyter notebook --ip= --port=8029 --no-browser


Copy the URL, and open it in a web browser. You can detach the "screen" session by pressing "ctrl-d".