Python Programming
Module Import
Directory Structure
Parent Folder
Coding Tips

How to import a module from the parent directory in Python

Codemia Knowledge Hub ·

Python does not search parent folders for modules by default, so a plain import mymodule fails when the file lives one level up. Either add the parent directory to sys.path before importing, or organise the code as packages (folders with __init__.py) and use relative imports.

Why a parent folder import fails

A module is a file of Python code (variables, functions, classes) that other programs can import. Shared code sometimes ends up in a parent folder, but Python's module resolution only lets a file import from the same folder or subfolders unless you take extra steps.

Method 1: Add the parent folder to sys.path

sys.path is the list of strings Python searches for modules. Insert the parent folder before importing:

python
1import sys
2import os
3
4# Append the parent folder path to sys.path
5parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
6sys.path.insert(0, parent_dir)
7
8# Now you can import the module from the parent folder
9import mymodule

Method 2: Use a relative path with ..

This variant spells out the relative path to the directory explicitly:

python
1import os
2import sys
3
4# Navigate up one directory from current script
5parent_dir = os.path.abspath('..')
6sys.path.insert(1, parent_dir)
7
8# Import the module located in the parent directory
9import mymodule

Method 3: Use packages and relative imports

If each folder is a package (it contains an __init__.py file), use a relative import:

python
# Inside your subdirectory file
from ..parent_module import some_function

This fits Python's own recommendations best and works especially well when you are developing packages.

Choosing an approach

Editing sys.path is a quick fix, but it is usually better to structure the project so shared dependencies sit in subdirectories rather than parent directories, or to use package-relative imports. Heavy use of sys.path changes has two costs:

  • Maintainability: the code becomes hard to understand and maintain.
  • Portability: different directory layouts can break imports when code moves between environments, such as from development to production.
MethodUse CaseProsCons
Modify sys.pathQuick fixes, small scriptsEasy to implementNot suitable for larger projects
Use os.path with ..Project with deeper directory structuresMore explicit directory navigationSlightly complex but clean
Use of __init__.pyStructured as packagesCleanest approach, very manageableRequires initial setup of package structure

For more depth, see Python's official documentation on modules and packages, including namespace packages, absolute vs relative imports, and the module cache (sys.modules).


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms