What importing a library means and why you need it

Importing a library in Python means loading code that someone else wrote so you can use it in your own program. Python comes with thousands of built-in libraries — collections of pre-written functions and tools — that handle common tasks like reading files, doing math, working with dates, or building web servers. Instead of writing all that code yourself, you write one or two lines at the top of your file to import what you need, and then you can use those functions immediately.

When you import a library, Python finds the file or folder containing that code, loads it into memory, and makes everything in it available to your program. The library stays loaded for as long as your program runs. You can import libraries that come with Python (called the standard library), libraries you installed separately (like NumPy or Django), or code you wrote yourself.

Key Takeaways

  • The simplest import is import math at the top of your file, which loads the entire math library and lets you call functions like math.sqrt().
  • Use from math import sqrt to import only one function, so you can call it as sqrt() instead of math.sqrt().
  • Give a library a shorter name with import numpy as np, which is especially useful for libraries with long names.
  • All imports must go at the top of your file, before you use any of the functions from those libraries.
  • If Python says "ModuleNotFoundError", the library either is not installed or Python cannot find it — check the library name and install it with pip if needed.

The basic import statement: loading an entire library

The simplest way to import is to write import followed by the library name. For example, import math loads Python's math library. After that line, you can use any function in the math library by typing the library name, a dot, and the function name.

Here is what that looks like in practice:

import math result = math.sqrt(16) print(result) # prints 4.0

The math library contains functions like sqrt() (square root), ceil() (round up), floor() (round down), and pi (the constant 3.14159...). When you import the whole library, you get access to all of them. The downside is that you have to type the library name every time, which gets tedious if you use one function many times.

This approach works for any library: import random, import datetime, import json. All of them follow the same pattern — import the name, then use it as a prefix.

Importing specific functions with "from"

If you only need one or two functions from a library, you can import just those functions instead of the whole library. Use the word from, then the library name, then import, then the function name.

from math import sqrt result = sqrt(16) print(result) # prints 4.0

Now you call sqrt() directly without typing math. first. This is cleaner if you use that function many times. You can also import multiple functions at once by separating them with commas:

from math import sqrt, ceil, floor print(sqrt(16)) # 4.0 print(ceil(4.3)) # 5 print(floor(4.7)) # 4

The trade-off is that someone reading your code might not immediately know where sqrt() came from. With import math and math.sqrt(), it is obvious. With just sqrt(), you have to look at the top of the file to see it came from the math library. For small programs this does not matter much, but in larger projects the import math style can make code easier to follow.

Renaming libraries with "as"

Some libraries have long names or names that are awkward to type repeatedly. You can give a library a shorter nickname using the word as. This is especially common with popular data science libraries.

import numpy as np import pandas as pd array = np.array([1, 2, 3]) dataframe = pd.read_csv('data.csv')

NumPy and Pandas are two of the most widely used Python libraries for working with numbers and data. Writing np.array() is much faster than writing numpy.array() every time, and it is the standard nickname that most Python programmers use. When you see np in someone else's code, you know they mean NumPy.

You can also rename functions when you import them:

from math import sqrt as square_root result = square_root(16)

This is less common and usually only useful if you have a name conflict — if two libraries have a function with the same name and you need both.

Where to put import statements in your file

All imports must go at the very top of your Python file, before any other code. Python reads your file from top to bottom, and it needs to load the libraries before it tries to use them. If you write import math halfway through your file, Python will not have loaded the math library when it reaches the earlier code that tries to use it.

The standard order is: first the imports from Python's standard library (like import math, import datetime), then a blank line, then imports from libraries you installed separately (like import numpy), then another blank line, then imports of your own code. This makes it easy to see at a glance what your program depends on.

import math import datetime import numpy import requests import my_custom_module

This is a convention, not a rule — Python will run your code even if you do not follow this order. But other programmers expect to see imports organized this way, so following it makes your code easier to read.

Installing libraries that do not come with Python

Python comes with about 200 libraries built in, but there are hundreds of thousands more available. To use a library that is not built in, you have to install it first using a tool called pip (which stands for "pip installs packages"). Pip comes with Python automatically, so you do not have to install it separately.

To install a library, open your terminal or command prompt and type pip install followed by the library name. For example, to install NumPy:

pip install numpy

Pip downloads the library from the internet and puts it in a folder where Python can find it. After that, you can import it like any other library. Common libraries you might install include NumPy (for math and arrays), Pandas (for working with data tables), Requests (for downloading web pages), and Django (for building websites).

If you are using Python 3 on a Mac or Linux computer, you might need to type pip3 instead of pip. If you are using a virtual environment (a separate Python setup for a specific project), make sure that environment is active before you run pip install.

What to do when you get a ModuleNotFoundError

If you try to import a library and Python says ModuleNotFoundError: No module named 'numpy' (or whatever library you tried to import), it means Python cannot find that library. This usually happens for one of three reasons: the library is not installed, you misspelled the name, or Python is looking in the wrong place.

First, check that you spelled the library name correctly. Common mistakes include writing import Numpy (capital N) instead of import numpy (lowercase), or writing import sk-learn instead of import sklearn. Library names are case-sensitive and sometimes do not match what you might expect.

If the spelling is correct, the library probably is not installed. Go back to your terminal and run pip install numpy (or whatever library you need). Wait for pip to finish, then try importing again. If you are working in a virtual environment or using a tool like Anaconda, make sure that environment is active before you run pip install — otherwise pip will install the library in the wrong place and Python will not find it.

Frequently Asked Questions

Can I import the same library twice in one file?

Yes, but Python will only load it once. If you write import math twice, the second line does nothing — Python recognizes that math is already loaded and skips it. There is no harm in doing this, but it is unnecessary and makes your code look messy.

What is the difference between import math and from math import sqrt?

With import math, you call functions as math.sqrt(). With from math import sqrt, you call it as sqrt(). The first style makes it clear where the function comes from; the second is shorter to type. Use whichever fits your code better.

Do I have to install libraries every time I run my program?

No. You install a library once with pip, and it stays installed. Every time you run your program, Python finds the already-installed library and loads it. You only need to run pip install again if you are on a different computer or in a different virtual environment.

Can I import code I wrote myself?

Yes. If you have a file called my_functions.py in the same folder as your main program, you can write import my_functions and use the functions inside it. Python treats your own files the same way it treats built-in libraries.

What does "import *" do?

Writing from math import * imports every function in the math library without needing the math. prefix. This is convenient for quick scripts but generally not recommended for larger programs, because it is hard to tell where functions come from and can cause name conflicts if two libraries have functions with the same name.