To import a local Python file, put the file in the same folder as the script you are running and use import filename (without the .py extension). If the file lives elsewhere, either add its folder to sys.path, adjust PYTHONPATH, or — the cleaner option for anything bigger than a quick script — turn the folder into a package with an __init__.py file and import from it. The rest of this guide walks through each approach in order, from the simplest to the most maintainable.

What You'll Need

  • Python 3 installed and working from a terminal (run python --version or python3 --version to check).
  • A text editor or IDE, or any way to write and save .py files.
  • Two files to experiment with: the module you want to import, and the script that imports it. Both can be one line long.
  • A basic idea of your project's folder layout, so you know where Python will look for files.

Step-by-Step

1. Create the file you want to import

Make a file called helpers.py in your project folder. Put something simple in it, such as a function:

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

Save it. The filename becomes the module name, so this module is called helpers.

2. Put the importing script in the same folder

Create main.py next to helpers.py. This is the single most important rule: when you run python main.py, Python adds the folder containing main.py to sys.path, which is the list of places it searches for imports. Files in that same folder are therefore found automatically.

3. Import and use it

import helpers

print(helpers.greet("Sam"))

Run python main.py. You should see Hello, Sam!. Notice there is no .py in the import statement — writing import helpers.py is a common error and will fail.

4. Choose the import style you want

  • import helpers keeps everything namespaced: call helpers.greet(). Best when the module has many functions.
  • from helpers import greet pulls one name in directly, so you call greet(). Convenient but can cause name clashes.
  • import helpers as h gives a short alias, useful for long module names.
  • from helpers import * imports everything public. Avoid it — it hides where names come from and can silently overwrite your own variables.

5. Handle a file in a subfolder

If helpers.py sits in a folder called lib, import it with a dotted path: from lib import helpers or import lib.helpers. The lib folder must itself be importable — in modern Python 3 a plain folder usually works, but adding an empty __init__.py file inside it makes the folder an explicit package and avoids ambiguity. That also prevents clashes with unrelated modules of the same name.

6. Import from a completely different folder

If the file is outside your project tree, you have three legitimate options:

  1. Add the folder to sys.path at the top of the script, before the import: import sys; sys.path.append("/path/to/folder"). Then import helpers works. Use an absolute path, or build one with pathlib relative to the script's own location so it does not break when you move the project.
  2. Set the PYTHONPATH environment variable to that folder before running Python. On macOS or Linux: PYTHONPATH=/path/to/folder python main.py. On Windows PowerShell: $env:PYTHONPATH="C:\path\to\folder" and then run the script. This is a per-session setting, not a permanent change.
  3. Install the folder as a proper package using a pyproject.toml and pip install -e .. This is the right choice for code you reuse across projects, because Python then finds it from anywhere without path hacks.

7. Verify it worked

Add print(helpers.__file__) temporarily. It prints the exact path Python loaded, which instantly tells you whether you are importing the file you think you are.

Common Mistakes

  • Including the extension. import helpers.py fails; the name is helpers.
  • Running from the wrong directory. The folder added to sys.path is the one containing the script you launched, not your terminal's current directory (for script runs). Launching with python -m behaves differently and uses the current directory.
  • Name clashes with standard library modules. Naming a file random.py, json.py, email.py or types.py shadows the built-in module and causes strange failures deep inside other libraries. Rename it.
  • Circular imports. Two modules importing each other at the top level can raise an ImportError. Move one import inside the function that needs it, or restructure so only one direction depends on the other.
  • Stale .pyc confusion. Python caches compiled bytecode in __pycache__. Editing the wrong copy of a file, or having two files with the same name in different folders, makes it look like changes are ignored.
  • Expecting edits to reload. A module is executed once per process. Changing the file mid-run does nothing until you restart the interpreter.

Tips

  • Give modules short, lowercase, descriptive names with no spaces. Underscores are fine.
  • Use if __name__ == "__main__": around test code in a module, so importing it does not accidentally run the tests.
  • Prefer packages and pip install -e . over scattering sys.path.append calls through a growing project — path hacks become fragile fast.
  • Keep a single top-level project folder and run scripts from inside it where possible; this removes most import surprises.
  • Be aware that sys.path.append adds the folder at the end, so a same-named module earlier in the path wins. sys.path.insert(0, ...) puts yours first if you truly need priority.

Troubleshooting

"ModuleNotFoundError: No module named 'helpers'" — this means Python searched and did not find it. Check that the file is really named helpers.py (not helpers.py.txt, a classic on Windows where extensions are hidden), that you are running the script from the folder you think, and that the spelling and case match exactly. Linux and macOS paths are case-sensitive.

It imports a different file than expected — print helpers.__file__. If the path is not yours, a file with the same name exists earlier on sys.path. Rename yours or reorder the path.

Edits have no effect — you are probably editing a different copy, or the module was imported earlier in the session. Restart Python. Delete __pycache__ if you suspect a stale cache, though Python normally invalidates it by timestamp.

ImportError mentioning partially initialised module — almost always circular imports. Trace which two files reference each other and break the loop by moving one import inside a function.

Permission or syntax errors inside the module — the error points at the imported file's line numbers, not your main script. Read the traceback to the bottom; that is where the real cause is.

Frequently Asked Questions

Do I need an __init__.py file?

In Python 3 it is not strictly required for a folder to be importable, but adding an empty one makes your intent explicit, avoids surprises with some tools, and prevents accidentally importing a same-named folder elsewhere. It is still good practice.

Why does it work in my IDE but fail in the terminal?

IDEs often add the project root to sys.path for you. The terminal does not. Either run scripts from the project root or run the entry point as a module with python -m package.script.

Can I import a file by its absolute path directly?

Yes, using importlib.util: build a spec with spec_from_file_location, create the module, and execute it. It works but it is fiddly; the sys.path or package approach is almost always cleaner.

What is the difference between a module and a package?

A module is a single .py file. A package is a folder of modules (usually with __init__.py) that you import with dotted names such as from lib import helpers.

Why does from helpers import * sometimes miss names?

The star import only brings in public names — those not starting with an underscore — or only what the module lists in __all__. It also makes code hard to read, so import names explicitly instead.

Can I import the same module under two different names?

You can, but Python caches modules by name, so the file executes once. Aliasing with import helpers as h simply gives you a second reference to the same module object.