Free tools Windows power users keep installed
One-click scans. No signup required.
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.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware match#1 Best Overall
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:
Rank #2
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:
Recommended Free Tools
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.
Best Value
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.
Quick Recap
Fix common import problems
ModuleNotFoundError: Check that the module name matches the filename stem, including capitalization, and that the containing directory is onsys.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.modulefrom 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.




