If you'd like your script to be more portable, consider finding the parent directory automatically:

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
Answer from user4490090 on Stack Overflow
🌐
Sentry
sentry.io β€Ί sentry answers β€Ί python β€Ί import files from a different folder in python
Python Import From Parent Directory and Subfolders | Sentry
Import modules from parent, child, and sibling directories using package relative imports with __init__.py or by adding paths to sys.path at runtime
Discussions

Import from parent directory
There's no nice and simple solution because there is no reason you should want to do that. Whatever it is you are trying to do, there's a official and better solution to use. However, if you really want to do it anyway, there's an easy hack. # b_file.py import sys sys.path.append("..") from a import a_file (this is the same as modifying PYTHONPATH, just doing it from within the python program instead of externally) More on reddit.com
🌐 r/learnpython
7
2
September 5, 2022
python - Import Script from a Parent Directory - Stack Overflow
How do I import a module(python file) that resides in the parent directory? Both directories have a __init__.py file in them but I still cannot import a file from the parent directory? In this fo... More on stackoverflow.com
🌐 stackoverflow.com
importing from parent directory
What are you actually trying to accomplish with this? I ask because it sounds like you are trying to do something that is not possible. This is because a submodule cannot be executed independently if it depends on a parent module. In the case you illustrate, foo.py depends on bar.py from a higher level module, so cannot be run on its own. It works with run.py because when you write from L1_dir.L2_dir.foo import *, python will first import the L1_dir module, then resolves the submodule L2_dir and imports that, then resolves the foo submodule and imports that. So in that case, by the time it gets to importing the foo submodule, it has already resolved the parent package and knows this is a child of it so no import error and all dependencies are good to go. You can workaround the import error by not using relative imports but all that is going to happen would be getting another exception indicating the module isn't found. As a side note, I should mention that using from x import * is generally bad practice as it can be unclear what is being imported and has potential for conflicting names. It's not that big of a deal when its only used to reference imports within your own package/module, but I find it's best to just avoid it so as not to get in the habit. More on reddit.com
🌐 r/learnpython
7
2
December 5, 2022
python - Import from sibling directory - Stack Overflow
I have a Python class called "ClassA" and another Python class which is supposed to import ClassA which is "ClassB". The directory structure is as follows: MainDir ../Dir ..../DirA/ClassA ..../DirB/ More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Where can I find more Python practice projects?
Browse itsourcecode.com Python Projects for 250+ free capstone-ready systems (sentiment analysis, image classification, chatbots, LangChain apps). Each includes full source code, dataset links, and installation instructions.
🌐
itsourcecode.com
itsourcecode.com β€Ί home β€Ί python import from parent directory in simple way
Python Import from Parent Directory in Simple Way (2026)
What Python version does this tutorial target?
This tutorial targets Python 3.10 or higher. Most examples work on 3.8+, but newer features (match statements, pipe union types, structural pattern matching) need 3.10+. For deep learning content, Python 3.11 is recommended for best performance.
🌐
itsourcecode.com
itsourcecode.com β€Ί home β€Ί python import from parent directory in simple way
Python Import from Parent Directory in Simple Way (2026)
How do I install Python for this tutorial?
Download Python 3.11 or higher from python.org. On Windows, tick 'Add to PATH' during install. On Mac use Homebrew (brew install python). On Linux use your package manager or pyenv for version management.
🌐
itsourcecode.com
itsourcecode.com β€Ί home β€Ί python import from parent directory in simple way
Python Import from Parent Directory in Simple Way (2026)
Top answer
1 of 16
760

You could use relative imports (Python >= 2.5):

from ... import nib

(What’s New in Python 2.5) PEP 328: Absolute and Relative Imports

2 of 16
562

I posted a similar answer also to the question regarding imports from sibling packages. You can see it here.

Solution without sys.path hacks

Summary

  • Wrap the code into one folder (e.g. packaged_stuff)
  • Create a pyproject.toml (older alternative: setup.py)
  • Pip install the package in editable state with pip install -e <myproject_folder>
  • Import using from packaged_stuff.modulename import function_name

Setup

I assume the same folder structure as in the question

.
└── ptdraft
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ nib.py
    └── simulations
        β”œβ”€β”€ __init__.py
        └── life
            β”œβ”€β”€ __init__.py
            └── life.py

I call the . the root folder, and in my case it is located in C:\tmp\test_imports.

Steps


1) Add a pyproject.toml to the root folder

The contents of the pyproject.toml can be simply

[project]
name = "ptdraft"
version = "0.1.0"
description = "My small project"

[build-system]
build-backend = "flit_core.buildapi"
requires = ["flit_core >=3.2,<4"]

Basically "any" valid pyproject.toml would work. This is just a minimal working example, which uses flit as build backend.


2) Use a virtual environment

If you are familiar with virtual environments, activate one, and skip to the next step. Usage of virtual environments are not absolutely required, but they will really help you out in the long run (when you have more than 1 project ongoing..). The most basic steps are (run in the root folder)

  • Create virtual env
    • python -m venv venv
  • Activate virtual env
    • . venv/bin/activate (Linux) or ./venv/Scripts/activate (Win)
  • Deactivate virtual env
    • deactivate (Linux)

To learn more about this, just Google out "python virtualenv tutorial" or similar. You probably never need any other commands than creating, activating and deactivating.

Once you have made and activated a virtual environment, your console should give the name of the virtual environment in parenthesis

PS C:\tmp\test_imports> python -m venv venv
PS C:\tmp\test_imports> .\venv\Scripts\activate
(venv) PS C:\tmp\test_imports>

3) pip install your project in editable state

Install your top level package (here ptdraft) using pip. The trick is to use the -e flag when doing the install. This way it is installed in an editable state, and all the edits made to the .py files will be automatically included in the installed package. Note that the -e flag with pyproject.toml requires pip 21.3 or newer.

In the root directory, run

pip install -e . (note the dot, it stands for "current directory")

You can also see that it is installed by using pip freeze

(venv) PS C:\tmp\test_imports> pip install -e .
Obtaining file:///home/user/projects/ptdraft
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Preparing editable metadata (pyproject.toml) ... done
....
Successfully built ptdraft
Installing collected packages: ptdraft
Successfully installed ptdraft-0.1.0
(venv) PS C:\tmp\test_imports> pip freeze
ptdraft==0.1.0

4) Import by prepending mainfolder to every import

In this example, the mainfolder would be ptdraft. This has the advantage that you will not run into name collisions with other module names (from python standard library or 3rd party modules).


Example Usage

nib.py

def function_from_nib():
    print('I am the return value from function_from_nib!')

life.py

from ptdraft.nib import function_from_nib

if __name__ == '__main__':
    function_from_nib()

Running life.py

(venv) PS C:\tmp\test_imports> python .\ptdraft\simulations\life\life.py
I am the return value from function_from_nib!
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-import-from-parent-directory
Python - Import from parent directory - GeeksforGeeks
July 23, 2025 - import sys # setting path sys.path.append('../parentdirectory') # importing from parentdirectory.geeks import geek_method # using geek_method() ... Here we will use the sys module as well as the path module for getting the directory and set the path directly to the required module. ... Parameter: Path: A path-like object representing a file system path.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί import from parent directory
r/learnpython on Reddit: Import from parent directory
September 5, 2022 -

Hello there :)

Assume, you have the following file structure:

.

└── a

β”œβ”€β”€ a_file.py

└── b

└── b_file.py

In b_file, I want to import functions from a_file. Even adding __init__.py to a/ did not help.

When searching for the problem, I found that I seemingly had to add b/ to PYTHONPATH or whatever. Please tell me there is a more convenient solution.

(If there's not a nice and simple solution, I'd be really interested in why it is not possible to simply import from a parent folder while it is no problem to import from a child directory.)

🌐
FavTutor
favtutor.com β€Ί blogs β€Ί import-from-parent-directory-python
How to Import File from Parent Directory in Python? (with code)
November 17, 2023 - Consider a scenario where you have a directory structure with a main script in the parent directory and a subdirectory containing additional Python files. If you try to import a module from the parent directory directly, you may encounter an ImportError or ModuleNotFoundError.
Find elsewhere
🌐
Syntx Scenarios
syntaxscenarios.com β€Ί home β€Ί python β€Ί how to import from parent directory in python? (top 6 ways)
How to Import from Parent Directory in Python? (Top 6 Ways)
August 16, 2026 - The cleanest option is to use relative imports inside a proper package with __init__.py files. If you just need a quick fix, you can add the parent folder to sys.path using sys, os, or pathlib.
🌐
Itsourcecode
itsourcecode.com β€Ί home β€Ί python import from parent directory in simple way
Python Import from Parent Directory in Simple Way (2026)
July 11, 2026 - Meanwhile, to import a file in Python, use the import statement. When importing from a parent directory in Python, set the path to the system’s absolute path.
🌐
GitHub
gist.github.com β€Ί JungeAlexander β€Ί 6ce0a5213f3af56d7369
Import modules from parent folder in Python Β· GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... Thanks! But it would also work to just do sys.path.insert(0, '..') and to omit the two lines before, right? ... Relative import does not seem to work since Python3.something: ValueError: attempted relative import beyond top-level package edit: both current and parent directories are packages (i.e.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί importing from parent directory
r/learnpython on Reddit: importing from parent directory
December 5, 2022 -

This question has been asked so many times but I'm still not finding what I need. If I call `./run.py` everything works fine. Calling `./foo.py` results in ImportError: attempted relative import with no known parent package. The only way I've found to accomplish this is to use sys.path.append which ends up making a mess of my imports. I don't think I want to use setup tools. Is there not some way to just do an absolute import from "package"? e.g. foo.py would have `import package.l1_dir.bar`

package 
|-> run.py 
|-> L1_dir
    |-> __init__.py
    |-> bar.py
    |-> L2_dir    
        |-> __init__.py
        |-> foo.py 

Foo.py

from ..bar import * print("hello from foo.py") 

Bar.py

print("hello from bar.py") 

Run.py

from .L1_dir.L2_dir.foo import *
Top answer
1 of 3
1
What are you actually trying to accomplish with this? I ask because it sounds like you are trying to do something that is not possible. This is because a submodule cannot be executed independently if it depends on a parent module. In the case you illustrate, foo.py depends on bar.py from a higher level module, so cannot be run on its own. It works with run.py because when you write from L1_dir.L2_dir.foo import *, python will first import the L1_dir module, then resolves the submodule L2_dir and imports that, then resolves the foo submodule and imports that. So in that case, by the time it gets to importing the foo submodule, it has already resolved the parent package and knows this is a child of it so no import error and all dependencies are good to go. You can workaround the import error by not using relative imports but all that is going to happen would be getting another exception indicating the module isn't found. As a side note, I should mention that using from x import * is generally bad practice as it can be unclear what is being imported and has potential for conflicting names. It's not that big of a deal when its only used to reference imports within your own package/module, but I find it's best to just avoid it so as not to get in the habit.
2 of 3
1
You can use python -m L1_dir.L2_dir.foo to execute it. However I'd advise to move your executable logic to a separate file, and do not use relative imports in that file. If the directory containing package is on the PYTHONPATH (sys.path), then you can write import package.L1_dir.bar from any file. The "correct" way to do this is to install the package via setuptools. If you use setuptools, you could also define an entrypoint; you can execute that entrypoint command from anywhere. IIRC it internally uses the -m semantics so shouldn't require extra sys.path or PYTHONPATH shenanigans.
🌐
Enterprise DNA
blog.enterprisedna.co β€Ί python-import-from-parent-directory
Enterprise DNA: We Help Businesses Put Data and AI to Work
Learn data and AI skills on the platform, bring us in on a project, or run a managed Omni Command Centre. 220K+ professionals trained, 1,500+ companies impacted globally.
🌐
Tutor Python
tutorpython.com β€Ί python-import-from-parent-directory
Python Import from parent directory - Tutor Python
October 19, 2023 - From the above figure, we can see that we have a main directory (kind of a root directory) named parent_directory. This directory contains another sub-directory current_directory, and a Python module named module2.py. The sub-directory, current_directory, contains a module named module1.py. It is from module1.py that we will be doing our imports from module2.py in the parent_directory.
🌐
Python Guides
pythonguides.com β€Ί python-import-from-parent-directory
How to Import Modules from a Parent Directory in Python
February 16, 2026 - By modifying sys.path at runtime, we tell the Python interpreter specifically where to look for our missing module. Here is how I implement this to pull state-specific configurations into our analysis script: import sys import os # Adding the parent directory to the system path # '..' represents the directory above the current file sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) # Now we can import our config file try: import config print("Successfully connected to the project configuration.") except ImportError as e: print(f"Connection failed: {e}") def run_sta
🌐
Delft Stack
delftstack.com β€Ί "delft stack" β€Ί "howto" β€Ί "python how-to's" β€Ί "how to import modules from parent directory in python"
How to Import Modules From Parent Directory in Python | Delft Stack
March 11, 2025 - If your project is structured as a package, you can use the __init__.py file to facilitate importing modules from a parent directory. This method is more organized and aligns with Python’s packaging guidelines.
🌐
Real Python
realpython.com β€Ί absolute-vs-relative-python-imports
Absolute vs Relative Imports in Python – Real Python
May 9, 2026 - You’d use only one dot here because module2.py is in the same directory as the current module, which is module1.py. You can import class1 and function2 into the package2/module3.py file this way: ... In the first import statement, the single dot means that you are importing class1 from the current package.
🌐
Lightly-dev
lightly-dev.com β€Ί blog β€Ί importing-from-parent-directory-python
How to Import Modules from Parent Directory in Python? - Lightly
July 4, 2023 - To import modules from the parent directory in Python, use sys.path.append("..") to add the parent directory to the module search path.
🌐
MLJAR
mljar.com β€Ί answers β€Ί import-files-from-different-folder-python
Import files from different folder in Python
To import files from parent or sibling directories, you can use package relative imports. First, you have to creaty an empty file named __init__.py in each directory to make them recognized as packages by Python:
🌐
Medium
medium.com β€Ί @teamcode20233 β€Ί importing-modules-from-parent-directory-using-python-fb6632f81682
Importing Modules from Parent Directory Using Python | by Teamcode | Medium
July 5, 2023 - To import a single module or packages from the parent directory using dot notation, you can follow these steps: Create an empty file called init.py in the parent directory. This file is necessary to mark the directory as a package.