
Downloading and Installing Packages

Install a Python package
- pip - (0:23)
- Install libraries, packages, and modules within Spyder - (0:50)
- Module - (1:27)
- Package - (2:30)
- Import modules and packages - (2:40)
What is pip?
Pip is a package management system used to install and manage Python packages. It is included in the installation of Anaconda.
The use of pip:
To install a package: pip install package
To uninstall a package: pip uninstall package
To upgrade a package: pip install --upgrade package OR pip install -U package
To search a package: pip search "package"
To list all packages installed: pip list
To get help for using pip: pip help
More pip methods can be found pip Docs.
How to install libraries / packages / modules?
First open Spyder and click Tools --> Open command prompt.
You should see the Command Window appear in the bottom right of your screen.
Here we install the Python package seaborn as an example.
# In the command line, type pip install seaborn
C:\Users\your_username\Documents\Python Scripts>pip install seaborn
This will install seaborn on your machine.
Note:
- To upgrade the pip version on Windows, type python -m pip install --upgrade pip on the command line.
- On Windows, all of your Python packages can be found in the directory of C:\Anaconda2\Lib\site-packages if you use the default path when you install Anaconda.
- To upgrade the pip version on OS X, type pip install --upgrade pip on the command line.
Modules and Packages
A module is simply a python file full of functions, classes, and variables; like all python files, it has the .py extension. We can use modules to group related codes. The dir() function can be used to see which functions are implemented in a module and help() can be used to see more detail about the function we want to use. Here is an example:
import math
dir(math)
Output: ['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin',
'asinh','atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees',
'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp',
'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
A package is a directory that consists of multiple modules and sub-packages.
dir() and help() can be used to explore packages as well.
Import modules or packages
Python provides several ways to import modules; we introduce three ways below.
- import math imports the module math. After we run this statement, we can use math.name to refer to functions defined in module math. For example, math.sin can be used to evaluate the value of trigonometric function sine and math.sin(math.pi/2) = 1.
- from math import * imports the module math. After we run this statement, we can simply use functions defined in module math without specifying the module they came from. For example, sin(pi/2) will evaluate sine of pi/2.
- from math import sin, cos imports the module math. After we run this statement, we can use sine and cosine without specifying the module, but not other functions in this module. For example, sin(math.pi/2) will evaluate sine of pi/2.
Which way is best?
We use modules in different ways depending on our needs. Sometimes, it is better to use import module, especially if we are using two modules that have a function of the same name - using this method will allow us to distinguish between the two. For example, numpy.sin can evaluate values of sine of an array but math.sin can only be used to evaluate the sine of a single value. An alternate example is the pi function: you can use math.pi, numpy.pi or scipy.pi and they all give the same value for pi. Because all three modules give pi the same value, we can choose one and have easy access to pi without having to import or specify another module.