Packages

How can we use functions other people define?

KEY TERMS

  • Packages: a named folder which gives you access to functions and code other people have written.

  • Importing a package: the process in which you upload the functions and code to your Jupyter notebook, so that you can use someone else's code.

  • Dot notation: reference the name of your package, followed with a dot (.) and the name of the function (or variable) you would like to use from within the package.

  • Whole package import: import all the information within a package.

  • Selective import: import one or more functions (or variables) within a package rather than the whole package.

Packages are a way to access functions and code other people have written. They group functions and variables together under the same name and can be accessed by referencing the name. They can reduce the amount of work you have to do, because they can define functions so you do not have to redefine them. To access the information in the package, you must first import the package.

By importing the package, you will be able to use the functions within that package. Packages must be imported into each Jupyter Notebook you open to use them. Once you have imported a package, you can use dot notation to access a function or variable within the package. Dot notation is where you call the name of your package followed with a dot (.) and the name of the function you would like to use.

For example, say we had a package called pkg, and we know it has a function named fix within it. To access the function, given that we had imported the package, we could write: pkg.fix() and it would call our function.

We use dot notation, so that if different packages have functions of the same name we will know which function we are using.

Importing Packages

There are multiple ways to import a package. The format in which you import a package depends on two things: what you want to use in the package and how you want to use it.

Whole Package Imports

A whole package import is when we import all the information within a package. After doing this import, you need to use your package name to reference the functions and variables you are importing. To do this you write:

import <package>

where <package> is replaced with the name of your package.

Let's say we had a package called pack and it was made up of the following code:

pack
named = "My name is ... "
def packing():    
    return 'packing'

In order to import our package named pack, we do the following line of code to access the functions and variables within pack.

import pack

Here is an image of what would happen in a Jupyter notebook when you try to use this package:

When using this type of import, if you do not reference the package from which you are accessing your function or variable, then your code will error.

If your packages has a long name, you can import your package under a new name. This will let you type less. You can give it any name, but it is best to use an abbreviation of some sort. For example, if you had a package named package, you can shorten the name. In the example above, we could do the following.

import pack as p

using this format we can call the following statements instead of the ones above.

>>> p.named
'My name is ..."

>>> p.packing()
'packing'

However, you must reference whichever name you use in your import statement. We can not call pack.named if we use import <package> as <name abbreviation>.

Selective Import

Selective import is where you only import the functions (or variables) within a package, that you want to use. To do this, use the following format:

from <package_name> import <function_name(s)>

The benefit to importing select functions rather than the whole file are that they can save you time and memory if you will only be using two functions from a package that has twenty functions.

This selective import will allows you to select multiple functions. Functions must be separated by commas.

## import a single part
from pack import packing

​​## Or import multiple parts
from pack import named, packing 

When selectively importing functions, you do not use dot notation. By doing this you will have direct access to the functions. You will not need to reference to the package, so the following works.

Summary

Packages are a great way to save time, because it allows the use of code that has already been written. The import methods above are only a few ways to use access packages. Here are some key things to remember:

  • If you import a whole package or a function within a package, it will influence whether you must reference a package each time you make a call to the function.

  • We can import packages and abbreviate their names.

Last updated