The most common way to import a file in Python

To import a file in Python, you use the import statement followed by the file name without the .py extension. If your file is named myfile.py and sits in the same folder as the script you're running, you type import myfile at the top of your code. Python then loads that file and makes everything in it available to use.

The file you're importing must be a Python file (ending in .py) and must be in a location Python knows to look — usually the same folder as your script, or in Python's standard library folders. When you import, Python runs the entire file once, then stores it in memory so later imports of the same file happen instantly.

If you only need one specific thing from a file — a function, a variable, or a class — use from instead. The line from myfile import my_function brings in only that one piece, keeping your code cleaner and faster.

Key Takeaways

  • Use import filename to load a Python file from your current folder, then access its contents with filename.function_name().
  • Use from filename import specific_thing to bring in only one function, class, or variable without loading the whole file's namespace.
  • Python looks for files in your current working directory first, then in standard library locations — check your file path if an import fails.
  • Imported files run completely the first time you import them, so any code at the top level of that file will execute.
  • Use import filename as alias to give an imported file a shorter or clearer name in your code.

Importing from the same folder

When your Python file and the file you want to import are in the same directory, the import is straightforward. Create a file called helpers.py with a function inside it, then in your main script write import helpers at the top. After that, call the function with helpers.function_name().

Here's a concrete example. If helpers.py contains:

def greet(name):   return f"Hello, {name}"

Then in your main script you write:

import helpers message = helpers.greet("Alice") print(message)

This prints "Hello, Alice". The key point: after import helpers, you must use the file name as a prefix to call anything inside it. If you try to call greet("Alice") directly without the helpers. prefix, Python will throw a NameError because it doesn't know where to find greet.

Using "from" to import specific items

If you only need one function or variable from a file, from is cleaner. The syntax is from filename import item_name. This brings that one item directly into your code's namespace, so you use it without a prefix.

Using the same helpers.py file from above, you could write:

from helpers import greet message = greet("Alice") print(message)

This does the same thing but lets you call greet() directly instead of helpers.greet(). You can import multiple items at once by separating them with commas: from helpers import greet, goodbye, welcome. This approach keeps your code shorter and makes it obvious which functions you're actually using.

Importing from subfolders

If the file you want to import is in a subfolder, you need to tell Python where to find it. Create a folder structure like this: your main script in the root, and a subfolder called utils containing helpers.py. To import from inside that subfolder, use a dot notation: from utils.helpers import greet.

The subfolder must contain a file named __init__.py (even if it's empty) for Python to recognize it as a package. Without that file, Python won't know the folder is meant to hold importable code. Once __init__.py exists, you can import from any Python file inside that folder using the dot syntax.

You can also nest deeper: if helpers.py is in utils/tools/, import it with from utils.tools.helpers import greet. Each level of the folder structure becomes another dot in the import statement.

Using aliases to rename imports

Sometimes a file name is long or unclear, or you want to avoid naming conflicts. Use the as keyword to give an import a shorter or different name. The syntax is import filename as short_name.

For example, if you have a file called database_utilities.py, you could write import database_utilities as db. Then you call functions with db.function_name() instead of typing out the full name every time. This also works with from imports: from helpers import greet as say_hello lets you call say_hello() instead of greet().

Aliases are especially useful when you're importing from multiple files that have functions with the same name, or when you're working with libraries that have long module names.

What happens when an import fails

The most common error is ModuleNotFoundError, which means Python couldn't find the file you asked for. This usually happens because the file isn't in the folder Python is looking in. Check that the file name is spelled exactly right (Python is case-sensitive on most systems), that it ends in .py, and that it's actually in your current working directory or in a subfolder with an __init__.py file.

If you're running your script from a different folder than where the file lives, Python won't find it. You can check your current working directory by adding import os and then print(os.getcwd()) to see where Python thinks it is. Move your script to the same folder as the file you're importing, or move the file to a subfolder and use the dot notation shown above.

Another error is ImportError, which means Python found the file but something inside it is broken — usually a syntax error. Read the error message carefully; it will point to the line in the imported file that has the problem. Fix that line, save the file, and try importing again.

Importing built-in and third-party libraries

Python comes with many built-in modules you can import without creating any files yourself. Common ones include os (for file and folder operations), sys (for system information), math (for mathematical functions), and random (for random number generation). Import them the same way: import os, then use os.function_name().

Third-party libraries — code written by other people and shared online — work the same way once they're installed on your computer. Libraries like requests (for downloading web pages) or pandas (for working with data) are installed using a tool called pip, then imported like any other file. The difference is that Python knows to look for them in a special libraries folder, not just your current directory.

Frequently Asked Questions

What's the difference between import and from import?

import filename loads the entire file and requires you to use the file name as a prefix when calling things inside it. from filename import item loads only that one item and lets you use it without a prefix. Use from when you need just one or two things; use import when you're using many things from the same file.

Do I need __init__.py in every folder?

Only in folders that contain Python files you want to import from. If a folder has __init__.py (even if it's empty), Python treats it as a package and lets you import from files inside it. Without it, Python won't recognize the folder as importable code.

Can I import a file from a different drive or completely different location?

Yes, but it's not recommended for most projects. You can add a folder to Python's search path using sys.path.append("/path/to/folder") at the top of your script, then import normally. For cleaner code, keep your files organized in subfolders relative to your main script instead.

What happens if I import the same file twice?

Python runs the file only the first time you import it, then reuses the loaded version for any later imports. This is why imported files run completely the first time — any code at the top level executes once, and functions or variables are defined once and reused.

Can I import a file with a name that's also a Python keyword?

No — Python won't let you create a file with a name like import.py or class.py because those are reserved words. Rename your file to something else, like import_data.py or class_definitions.py, and import that instead.