Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Import Module Syntax
  • Examples
  • Now you try it!
  1. Language Basics

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!

PreviousLesson 14. FunctionsNextLesson 16. Python Programming Standards

Last updated 3 years ago