What importing a file means in Python

Importing a file in Python means loading code from another file so you can use it in the file you are working on right now. When you write Python code, you often need functions or data that someone else has already written. Instead of rewriting that code, you import it — you tell Python to go find that file and bring its contents into your current project.

Python has two main ways to import: you can import built-in modules that come with Python itself (like a calendar or math functions), or you can import files you created yourself or that someone else shared with you. The most common method is using the import statement at the top of your file.

Key Takeaways

  • The import statement at the top of your file tells Python to load code from another file or module so you can use it.
  • Built-in modules like math, random, and datetime come with Python and do not require installation.
  • To import your own file, both files must be in the same folder, and you write import filename without the .py extension.
  • You can import a single function from a file using from filename import function_name, which saves you from typing the filename every time you use it.
  • If a module is not built-in, you may need to install it first using a tool called pip before you can import it.

Importing built-in modules that come with Python

Python includes modules for common tasks — math operations, working with dates, generating random numbers, and more. These are already on your computer when you install Python, so you do not need to read anything. To use one, write import followed by the module name at the very top of your file.

For example, if you want to use the math module to calculate a square root, you write:

import math result = math.sqrt(16) print(result)

When you run this code, Python finds the math module, loads it, and then you can use any function inside it by typing the module name, a dot, and the function name. Other common built-in modules are random (for picking random numbers), datetime (for working with dates and times), and os (for working with files and folders on your computer).

Importing your own files

If you created a Python file with functions or data you want to use in another file, you can import it the same way. The key rule is that both files must be in the same folder on your computer. If they are, you write import followed by the filename without the .py extension.

Say you have a file called helpers.py that contains a function called greet_user. In another file in the same folder, you would write:

import helpers helpers.greet_user("Alice")

Python looks for a file named helpers.py, loads it, and then you call the function by typing the filename, a dot, and the function name. If the files are in different folders, you need to set up the folder structure differently, which is more advanced — for now, keep imported files in the same folder.

Importing just one function from a file

Sometimes you only need one function from a file, and typing the filename every time gets repetitive. You can import just that function using from and import together. This brings the function directly into your file so you can use it by name alone.

Using the helpers.py example again, you could write:

from helpers import greet_user greet_user("Alice")

Now you call greet_user directly without typing helpers. first. You can also import multiple functions at once by separating them with commas:

from helpers import greet_user, calculate_age, format_name

This style is cleaner when you are using just one or two things from a file. If you are using many functions from the same file, regular import is often clearer.

Installing and importing modules from outside sources

Many programmers share useful modules that do not come with Python. These are stored in a central library called PyPI (Python Package Index). To use one, you first install it using a tool called pip, which comes with Python automatically.

Open your terminal or command prompt and type:

pip install module_name

For example, to install a popular module called requests (used for downloading data from websites), you would type pip install requests. Once it finishes, you import it the same way as a built-in module:

import requests response = requests.get("https://example.com")

If pip does not work, you may need to use pip3 instead, depending on how Python is set up on your computer. The installation only needs to happen once — after that, you can import the module in any file.

Common mistakes when importing

The most common mistake is forgetting that both files need to be in the same folder. If you try to import a file that is in a different folder, Python will not find it and you will get an error message saying the module does not exist. Check that both files are in the same directory before you import.

Another mistake is including the .py extension when you import. You write import helpers, not import helpers.py. Python already knows to look for a .py file.

A third mistake is trying to import a module that is not installed. If you get an error saying "No module named [something]", it usually means you need to run pip install first. Check the module's documentation to find the exact name to install — sometimes the name you import is different from the name you install.

Frequently Asked Questions

Can I import a file from a different folder?

Yes, but it requires extra setup. You can add the folder path to Python's search list using sys.path, or you can organize your files into packages using a special __init__.py file. For beginners, keeping files in the same folder is simpler and works fine for most projects.

What is the difference between import and from import?

import helpers loads the entire file and you call functions as helpers.function_name. from helpers import function_name brings just that function into your file so you call it as function_name. Use the first when importing many things; use the second when importing one or two.

Do I have to install built-in modules?

No. Built-in modules like math, random, and datetime come with Python and are ready to use when ready. You only use pip to install modules from outside sources that do not come with Python.

What does the error "ModuleNotFoundError" mean?

It means Python could not find the module you tried to import. Check that the filename is spelled correctly, that both files are in the same folder, or that you have installed the module using pip if it is from an outside source.