To import a module that sits in the same folder as your script, save the file with a .py extension and import it by its bare name, without the extension or any path: put helper.py next to main.py, then write import helper or from helper import some_function at the top of main.py. Python automatically adds the folder of the script being run to its search path, so the import works with no configuration, as long as the two files really are siblings and you launch the script in the normal way (for example python main.py from any directory).
What You'll Need
- Python 3 installed and working from a terminal or command prompt (run
python --versionorpython3 --versionto confirm). - Two files in the same directory: the one you are running, and the one you want to import.
- A text editor or IDE, and a terminal open in the project folder so you can see error messages clearly.
- A basic grasp of what a module is: any
.pyfile can be imported by other files.
Step-by-Step
- Confirm the files are in the same folder. On Windows, run
dir; on macOS or Linux, runlsin the folder. Both filenames should appear in that single listing. If the imported file is inside a subfolder, this method will not find it and you need a package instead (see Common Mistakes). - Name the imported file in lowercase with underscores. Use
data_helpers.pyrather thanData Helpers.py. Spaces, hyphens and inconsistent capitalisation cause import errors or confusing behaviour, especially when moving between operating systems. - Put importable code in the file. Functions and classes at the top level are ready to use. If the file also does something when run (printing a report, for example), guard that part with
if __name__ == "__main__":so it only fires when the file is run directly, not when it is imported. - Import it at the top of the other script. Write
import data_helpers. This loads the whole module and you then calldata_helpers.clean_text(), keeping names namespaced. Usefrom data_helpers import clean_textif you want the function available directly, which is shorter but makes it harder to see where a name came from in larger files. - Run the scripts from the folder that holds them, or use
-m. Runningpython main.pyfrom any directory works because Python adds the script's own directory tosys.path. If you are inside a package, the safer pattern ispython -m package.mainfrom the parent folder, which keeps imports consistent. - Check it works. Print something from the imported module to prove the link. If no error appears and you see the function's output, the import is correct. When it fails, read the traceback carefully:
ModuleNotFoundErrorgenerally means the name or location is wrong, whileImportErrorwith a name usually means the function or variable does not exist in that file. - Reinstall or refresh if you renamed files. After renaming a file or moving it, delete any
__pycache__folder in the directory (it is regenerated automatically and safe to remove) and rerun. Python caches compiled bytecode by filename, so stale caches sometimes confuse you during refactors.
Common Mistakes
- Including the extension:
import helper.pyis invalid syntax. Importhelperonly. - Using a path:
import ./helperorimport myfolder.helperfails unlessmyfolderis a real package with an__init__.pyfile, or a namespace package your import path expects. Same-folder imports do not need paths. - Naming a file after a standard library or installed package. A local
random.py,json.pyoremail.pyshadows the real module, soimport randomloads your file instead and breaks third-party code. Rename it. - Circular imports. If
a.pyimportsb.pyandb.pyimportsa.pyat the top level, one of them gets a half-built module. Move the import inside the function that needs it, or restructure so that one file depends on the other only. - Running a file from the wrong place with an IDE. Some editors set the working directory differently than your terminal. Use the IDE's run configuration to set the working directory to the script's folder, or you may see imports work in one place and fail in another.
- Unintended side effects. Without a
__main__guard, importing a file runs all its top-level code, printing output or even launching work you did not ask for.
Tips
- Keep same-folder imports deliberate: a flat folder of a dozen modules is fine, but once it grows, group related files into a package with an
__init__.pyand importfrom package import module. - Use
import module as aliasfor long names, andfrom module import x as ysparingly, since renamed imports get hard to trace. - Avoid
from module import *. It dumps every public name into your namespace and hides where functions come from, and it silently changes behaviour if the module adds new names later. - Print
sys.pathwhen an import fails. It shows exactly which folders Python searched, which immediately tells you whether your folder is on the path. - Give modules plain, lowercase names and keep each one focused. The import itself is trivially easy; most failures come from names and layout.
Troubleshooting
ModuleNotFoundError: No module named 'helper'. The file is not in the directory Python searched, or the name is misspelled. Confirm with ls/dir that it is beside the running script, check capitalisation, and print sys.path. If the script is launched from elsewhere, run it as python path/to/main.py.
ImportError: cannot import name 'clean_text'. The file was found but does not define that name, or it is defined inside a function or class. Check spelling and indentation. This also happens with circular imports, where the module is loaded only partway before the failing import.
It works in the terminal but not in your editor or notebook. Notebooks and some IDE run buttons set the working directory to the project root, not the script's folder. Change the working directory to the script's folder, or add the folder to the project root so both see the same path.
Wrong module loaded. A same-named file elsewhere or a local file shadowing a standard library name is being imported. Rename files, and clear __pycache__ to rule out stale bytecode.
Still stuck. Add one line at the top of the failing script: print(__file__). It shows the exact file being run, which is often not the copy you edited.
Frequently Asked Questions
Do I need to install anything to import a file from the same folder?
No. Python adds the running script's directory to its search path automatically. No package, environment variable or configuration file is required.
Can I import a file with a hyphen or a space in its name?
Not with a normal import statement, because names with hyphens or spaces are not valid Python identifiers. Rename the file using underscores instead. Loading unusual names with importlib is possible but rarely worth the complexity.
Why does importing my file print output I did not expect?
Because importing executes the file's top-level code. Wrap any test or run code in if __name__ == "__main__": so it only runs when the file is executed directly.
What is the difference between import and from ... import?
import helper loads the module and requires you to use helper.function(). from helper import function brings the name into your current file, which is shorter but less explicit. Both find the same file.
Can I import a file from one folder up or from a subfolder?
Not by bare name. For a subfolder, make it a package with an __init__.py and import from subfolder import module. For a parent folder, run from that folder or use python -m. Same-folder imports are the simplest case precisely because no path is involved.
Should I add the folder to the path manually?
Usually not. Modifying sys.path inside a script is a workaround, not a fix. Correct file layout and the right working directory solve the problem more reliably.