Tutorials
Search…
Lesson 15. Importing Modules
When the python interpreter starts up, there are a bunch of basic things that are loaded into what is called the namespace. The namespace is a bunch of key value pairs that allow you to write less verbose code.
Remember how I said everything in Python is an object and how the term class was synonymous with object? The Python interpreter itself is a software program made up of many classes. When you are writing Python, what you are doing is calling these classes.
When you call a class, you have to fully identify the class you are calling. If the function that you want is buried deep in interpreter software, that could get to be really annoying to have to write out every single object name until you got down to what you wanted.
What the namespace does is bring in references to those objects so you can access them directly without having to write a bunch of code.
You have been doing this the entire time without really thinking about it. The tutorial up to this point has been teaching you the base functionality of Python. All that functionality is imported into the namespace so you can do things like call str() really easily.
But ground school is almost over. I’ve taught you most of the basics of the language necessary to start doing things in Python. Pretty soon, we gotta kick the tires and light the fires, and put all of this newfound knowledge to use. That is going to require bringing in more references to Python objects than what gets loaded by default.
The namespace takes up space in memory. We want to be efficient with our code, so we only want to load up what is absolutely necessary. This is where the import statement comes in.
You have already seen it in lesson three when we were talking about creating decimal values. The decimal module is not loaded into the namespace by default. We had to import from the decimal module so we could get the Decimal object.
Installing Modules
At some point, you are going to have to import a module that is not already installed. When that happens, you will have to download the module's package from PyPi, the Python Package Index.
PyPi is a repository for Python packages. In order to install packages from PyPi, you will use a command line tool called pip.
pip will grab the package from the cloud and install it for you. When we get to doing data science with Python, there will be a lot of pip installing involved.

Import Module Syntax

This is not an exhaustive list of import methodology. This is just the most frequently used way to skin the cat. In the last example, you can see that * is being used as a wildcard operator to get everything in that module. This is lazy and not recommended. I do it all the time.
import [module]
from [module] import [function]
import [module] as [alias]
from [module] import *

Examples

Example #1: Foreshadowing
Hi kids! Do you like data science? Get ready. You are going to be seeing the below a lot. I mean like A LOT!
import numpy as np
import pandas as np
Example #2: Installing Packages With pip
When we start working with databases, we are going to need a module called pyodbc.
Open Anaconda Prompt as administrator and run the following command.
pip install pyodbc
Example #3: Installing Packages With Normal Command Line
So there you are jammin’ on some code in JupyterLab when, all of a sudden, you realize that you need to install a package.
BUT WAIT! Anaconda Prompt is running your kernel and you can’t have more than one instance of Anaconda Prompt open at a time. You don’t want to shut down your kernel because that is crazy talk. So what do you do?
WHAT? DO? YOU? DO?
Fortunately, we won’t have to shoot the hostage. Just open up your normal command line utility for your OS as administrator and make the following adjustments to example 2 to run pip in the normal command line.
First, tell your machine that you want to work with Python. Then, let it know that you’re about to invoke a module by using the -m flag. After that, everything else is normal.
python -m pip install pyodbc

Now you try it!

Don't copy and past. Type the code yourself!
Copy link
On this page
Import Module Syntax
Examples
Now you try it!