DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Import a Python File from the Same Directory in Python

Use a neighboring file’s module name without .py. The correct import and launch method depend on whether the files are loose scripts or package modules.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For two ordinary Python files in the same directory, import the other file by its filename without the .py extension. If main.py and helper.py sit beside each other, write import helper or from helper import useful_function in main.py. Whether that works depends on how the program is launched and, for package code, whether Python knows the module’s package.

Import a neighboring .py file

Suppose your directory contains these two files:

project/
├── main.py
└── helper.py

In main.py, import the filename stem—helper, not helper.py:

import helper

helper.some_function()

Or import a particular function or other name directly:

from helper import useful_function

useful_function()

Python makes the directory containing a directly run script the first entry on its module search path, so this simple import normally works when you start the program with python path/to/main.py. The Python 3.14 command-line documentation describes this behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the launch method matters

Python searches the locations in sys.path when resolving an import. With a script passed directly to Python, the script’s directory is first. In an interactive session, with -c, or with -m, the initial path instead uses the current working directory when there is no input script directory. See the Python 3.14 path initialization reference.

This is why “same directory” can be misleading in an IDE, notebook, test runner, or embedded interpreter: the working directory and the file’s directory may differ, and the launcher may set paths differently. To see the first search location in the running process, temporarily print:

import sys
print(sys.path[0])

If Python cannot find the module, first check the filename spelling and capitalization, then confirm the directory containing it is on sys.path for that launch method. Avoid adding a directory to sys.path as a reflexive permanent fix; the right import depends on whether these are loose files or part of a package.

Use relative imports for modules in a package

If the files live inside a package, use a package-aware import. For example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
project/
└── mypackage/
    ├── __init__.py
    ├── main.py
    └── helper.py

From mypackage/main.py, a sibling import can be written as:

from . import helper
# or
from .helper import useful_function

Leading-dot imports are relative imports: they rely on Python knowing the current module’s package. The Python 3.13 tutorial’s intra-package reference explains this package context and notes that the main module has no package. Accordingly, launching the file directly with python mypackage/main.py makes it __main__ without package identity; a relative import can fail with “attempted relative import with no known parent package.”

For package code, run the module from the project’s parent directory using -m:

python -m mypackage.main

The Python 3.14 command-line reference documents that -m locates the named module through the standard import mechanism. The official __main__ documentation also demonstrates running a package with python -m and importing a sibling module relatively.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Prevent imports from running script-only behavior

Python executes a module’s top-level statements when it is first imported. If the file also needs a command-line entry point, keep that startup behavior behind a main guard:

def main():
    print("Run the program")

if __name__ == "__main__":
    main()

When Python imports the file, its module name is not __main__, so the guarded call does not run. When you execute that file as the top-level script, the guard does run. This lets a module provide reusable definitions without triggering its application startup just because another file imported it; see the Python Modules tutorial and __main__ documentation.

Fix common import problems

  • ModuleNotFoundError: Check that the module name matches the filename stem, including capitalization, and that the containing directory is on sys.path. The launch mode determines which directory Python starts with.
  • Relative-import error: Use relative imports within a package and launch package code with python -m package.module from the package’s parent directory, rather than running the package file directly.
  • Unexpected code runs on import: Move script startup into an if __name__ == "__main__": block.
  • The wrong module loads: A script’s directory is early on the search path. Avoid naming a local file after a standard-library module or dependency unless that shadowing is intentional.
  • Edits do not appear in an interactive session: Python caches imported modules in that interpreter. Restart the session or explicitly reload the module after editing.

Choose the import that matches your files

Situation Import pattern Typical launch
Loose files beside one another import helper or from helper import useful_function python main.py
Sibling modules within a package from . import helper or from .helper import useful_function python -m mypackage.main from the project’s parent directory

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.