Python modules can be generalized to
You've been using them all day. Can you think of an example?
import numpy
import matplotlib
import multiprocessing
import sys
Modules like multiprocessing and sys are standard modules, but packages like numpy and matplotlib are non-standard. You can usually tell when they have their own external site.
Notice that the standard ones both come from the python.org site.
Lets say you wanted to use the pysam module
http://pysam.readthedocs.io/en/latest/installation.html
to work with a sam file in python. The documentation says that we only need to run
pip install pysam
We can run that on the shell through our notebook by prepending a !
.
!pip search pysam
!pip install pysam
Why didn't that work?
If you take a look at the error, you can see that we are getting a permission error.
PermissionError: [Errno 13] Permission denied: '/opt/conda/lib/python3.5/site-packages/pysam'
This type of error happens when you are trying to write files in a location you do not have access to. Remember that we're all sharing the same computer system, so we want to provide a consistent environment between users and sessions. Earlier this week, you did learn that you can compile and run code from your own directories, and python has the same capability. Python even makes it simple.
By default, python will look for local modules installed in
$HOME/.local/lib/pythonX.Y/site-packages
You can also ask pip to install packages there instead of system-wide directories that you don't own by using the --user
flag.
(https://pip.readthedocs.io/en/latest/user_guide/#user-installs)
!pip install --user pysam
!pip list --user
import pysam
The package is installed, but you will need to restart your kernel for the your local path to be loaded. Please
You only need to do this once since your $HOME/.local folder did not exit before.
import pysam
Give it a try with biopython or another package
!pip search biopython
!pip install --user biopython
import Bio
Works!
If you ever need to do a manual install of a python package outside of pip, the --user
flag exists there as well.
(https://docs.python.org/2/install/#alternate-installation-the-user-scheme)
While not necessary at all, you can clean up your environment by uninstalling these packages with
!pip uninstall -y biopython pysam