Importing Modules
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
Once you have installed Python modules using PIP, the next step is to import them into your Python code. Importing modules allows you to leverage the functionality provided by external packages, expanding the capabilities of your programs. Here's a step-by-step guide on how to import modules with PIP:
Ensure that the desired module is already installed on your system. You can use the pip list command to check the installed modules.
Open your Python script in a text editor or integrated development environment (IDE).
At the top of your script, import the module using the import statement. For example, to import the requests module:
import requests
This statement makes all the functionality of the requests module available within your script.
Once imported, you can use the module's functions, classes, or variables in your code. For example, to make an HTTP GET request using the requests module:
import requests
response = requests.get("https://api.example.com")
print(response.status_code)
In this code snippet, we import the requests module and use its get function to make an HTTP GET request. We then access the status_code attribute of the response object and print it to the console.
If a module has a long or complex name, you can use the as keyword to assign it a shorter alias. This can make your code more readable. For example:
import matplotlib.pyplot as plt
# Plotting code using the Matplotlib module
In this example, we import the matplotlib.pyplot module and assign it the alias plt. This allows us to refer to the module using the shorter name plt in our code.
Some modules have submodules or nested structures. To import specific parts of a module, you can use the from ... import statement. For example, to import only the datetime class from the datetime module:
from datetime import datetime
now = datetime.now()
print(now)
This code snippet imports only the datetime class from the datetime module, allowing us to directly use the class without referencing the module name.
By following these steps, you can effectively import and utilize Python modules installed with PIP. Importing modules extends the functionality of your Python projects and empowers you to leverage the vast ecosystem of open-source libraries and packages available. Experiment with different modules and explore their documentation to unlock their full potential in your Python programming endeavors.
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 15min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 7 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 7 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 36min
0 / 8 lessons complete
Section Overview
Text | 3 min
Casting
Text | 3 min
Strings
Text | 4 min
Booleans
Text | 7 min
User Input
Text | 2 min
Numbers
Text | 6 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 38min
0 / 6 lessons complete
Python Lists
Text | 7 min
Python Tuples
Text | 7 min
Python Sets
Text | 6 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 4 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 29min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 3 min
Assignment: Rock, paper, scissors
Text | 13 min
Assignment: Count Dictionary Words
Text | 6 min
Python Loops • 20min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 4 min
While Loops
Text | 4 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 7 min
Python PIP and Modules • 15min
0 / 4 lessons complete
RegEx • 23min
0 / 4 lessons complete
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete
