What importing a library means and why you need it
A library in Python is a collection of pre-written code that does a specific job — handling dates, working with files, doing math, talking to the internet. Instead of writing that code yourself, you import the library and use what's already there. Importing tells Python to load that code into your program so you can use it.
When you import a library, Python looks for it in a few standard places on your computer. If it finds it, the code becomes available to you. If it doesn't find it, you get an error. Most libraries come built into Python (called the standard library), but some you have to read first.
The simplest import looks like this: import math. That single line loads Python's math library, and now you can use functions like math.sqrt() to find square roots or math.pi to get the value of pi.
Key Takeaways
- The four main import patterns are: import the whole library, import a specific function, import everything from a library, and import a library with a shorter name.
- Built-in libraries like math, random, and datetime come with Python and need no read; third-party libraries like requests or pandas must be downloaded first using pip.
- Put all your import statements at the very top of your Python file, before any other code, so Python loads everything before it runs anything else.
- If an import fails, the most common reason is that a third-party library was never downloaded, which you fix by running pip install in your terminal.
The four ways to import and when to use each one
The most straightforward import brings in an entire library. Write import math at the top of your file, and then use math.sqrt(16) or math.pi anywhere below. You type the library name, then a dot, then the function you want. This style is clear because anyone reading your code knows that sqrt came from the math library.
The second pattern imports only what you need: from math import sqrt. Now you can write sqrt(16) without the math. prefix. This is useful when you know exactly which functions you'll use and want shorter code. The downside is that someone reading your code might not when ready know where sqrt came from.
The third pattern imports everything at once: from math import *. This loads every function in the library so you can use them without a prefix. Most experienced programmers avoid this because it can cause confusion — if two libraries have a function with the same name, one will overwrite the other and you might not notice.
The fourth pattern gives a library a shorter nickname: import numpy as np. This is especially common with libraries that have long names. Instead of typing numpy.array() every time, you type np.array(). This is standard practice for popular libraries like numpy, pandas, and matplotlib.
Built-in libraries that come with Python
Python includes dozens of libraries you can import when ready without downloading anything. The math library has functions for square roots, trigonometry, and constants like pi and e. The random library generates random numbers and picks random items from lists. The datetime library works with dates and times — creating them, comparing them, formatting them for display.
The os library lets you interact with your computer's file system — listing files in a folder, creating directories, checking if a file exists. The sys library gives you information about Python itself and how your program is running. The json library reads and writes JSON data, which is a common format for storing and sending information.
You can see a full list of built-in libraries in Python's official documentation, but you don't need to memorize them. When you need to do something specific, search for which library handles it, then import that library and look up the functions inside it.
Third-party libraries and how to read them
Libraries that don't come with Python are called third-party libraries. Popular ones include requests (for downloading web pages), pandas (for working with data tables), matplotlib (for drawing charts), and flask (for building websites). Before you can import these, you have to read them.
The tool for downloading libraries is called pip. It comes with Python automatically. To read a library, open your terminal or command prompt and type: pip install requests (replacing "requests" with whatever library you need). Pip downloads the library and puts it in a standard location on your computer where Python knows to look for it.
After pip finishes, you can import the library in your Python code just like a built-in one. If the import fails, the most common reason is that pip didn't finish successfully — check the terminal output for error messages, or try running the pip install command again.
Where to put import statements in your file
All import statements should go at the very top of your Python file, before any other code. Python reads your file from top to bottom, so it needs to load the libraries before it tries to use them. If you write code that uses a library before you import it, Python will crash with a NameError.
Group your imports in this order: first the built-in libraries, then a blank line, then the third-party libraries. This makes it straightforward to see at a glance what your program depends on. For example:
import math import random import datetime import requests import pandas as pd
Some projects use many libraries, so the import section can be long. That's normal and expected. Anyone reading your code will look at the imports first to understand what tools you're using.
What to do when an import fails
The most common error is ModuleNotFoundError: No module named 'requests' (or whatever library you tried to import). This means Python looked for the library and couldn't find it. For built-in libraries, this usually means you misspelled the name — check that it matches exactly. For third-party libraries, it means you haven't downloaded it yet with pip.
Open your terminal and run pip install requests (using the correct library name). Wait for it to finish, then try your Python code again. If pip says it already installed the library but Python still can't find it, you might have multiple versions of Python on your computer and pip installed it for the wrong one — this is rare but happens sometimes.
Another possibility is that you're in the wrong folder. Some projects create isolated environments where libraries are stored separately. If you're working in such a project and pip install doesn't seem to work, ask whoever set up the project how to set up the environment first.
Frequently Asked Questions
Can I import the same library twice in one file?
Yes, but Python ignores the second import. Once a library is loaded, importing it again does nothing. You can write import math twice and it won't cause an error, but it's pointless — just import it once at the top.
What's the difference between a library and a module?
A module is a single Python file with code in it. A library is a collection of modules organized together. In practice, people use the words interchangeably, but technically a library contains modules. When you import, you're usually importing a library that contains multiple modules inside it.
Do I have to use pip to read libraries, or are there other ways?
Pip is the standard tool and works for almost everything. Some projects use other tools like conda or poetry, but those are less common. Start with pip — if it doesn't work for what you're trying to do, the project documentation will tell you which tool to use instead.
What happens if I import a library I don't use?
Nothing bad happens. Your program will run fine. The library just sits there loaded in memory but unused. It's not a problem for small programs, but in large projects people clean up unused imports to keep the code tidy and make it clear what the program actually depends on.
Can I import a library with a different name than what it's actually called?
Yes, using the as keyword. You can write import numpy as np or import pandas as pd. You can even write import math as m if you wanted to, though that would be confusing. The name you choose is just for your code — it doesn't change the library itself.