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:
Method 2: Use a relative path with ..
This variant spells out the relative path to the directory explicitly:
Method 3: Use packages and relative imports
If each folder is a package (it contains an __init__.py file), use a relative import:
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.
| Method | Use Case | Pros | Cons |
Modify sys.path | Quick fixes, small scripts | Easy to implement | Not suitable for larger projects |
Use os.path with .. | Project with deeper directory structures | More explicit directory navigation | Slightly complex but clean |
Use of __init__.py | Structured as packages | Cleanest approach, very manageable | Requires 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).
Related reading
- Importing TensorFlow fails with a SyntaxError, complaining about a parameter called async
- Importing tensorflow makes python 3.6.5 error
- Importing variables from another file?
- ImproperlyConfiguredError about app_name when using namespace in include
- Improve subplot size/spacing with many subplots
- Impute entire DataFrame all columns using Scikit-learn sklearn without iterating over columns
- In Flask convert form POST object into a representation suitable for mongodb
- In Javascript / ES6, how do I wait for Python code to finish executing in a Jupyter Notebook?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.