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

from ... import nib

(Whatโ€™s New in Python 2.5) PEP 328: Absolute and Relative Imports

Answer from f3lix on Stack Overflow
๐ŸŒ
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.)

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-import-from-parent-directory
Python - Import from parent directory - GeeksforGeeks
July 23, 2025 - From Python 3.3, referencing or importing a module in the parent directory is not allowed, From the below example you can clearly understand this. In the parent directory, we have a subdirectory, geeks.py file and in the subdirectory, we have ...
Discussions

python - Importing modules from parent folder - Stack Overflow
There are solutions that work out the current directory of the file and import it from the parent folder that way as well. 2015-11-14T10:59:08.15Z+00:00 ... @Shuklaswag for example, the answer stackoverflow.com/questions/714063/โ€ฆ. 2018-09-22T09:38:34.69Z+00:00 ... Save this answer. ... Show activity on this post. The pathlib library (included with >= Python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Import file from parent directory causes "Import by filename is not supported" - Stack Overflow
I written a script py-run.py, which ... relative imports without the need for sys.path.append. It achieves this by dynamically constructing the parent module path and running the Python file from the top-level module. This is particularly useful when your script relies on modules located in parent directories and you want ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to do absolute import (a parent folder from the child folder)
The structure of my package is given below package_test main.py __init__.py folder_1 test_1.py __init__.py The main.py contains something from folder_1.test_1 import params if __name__ == "โ€ฆ More on discuss.python.org
๐ŸŒ discuss.python.org
1
0
July 13, 2021
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
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)
๐ŸŒ
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
๐ŸŒ
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.
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!
๐ŸŒ
Maxtrain
maxtrain.com โ€บ home โ€บ how to import from parent directory python
How To Import From Parent Directory Python - MAX Technical Training
March 4, 2024 - Include an empty file named โ€œ__init__.pyโ€ in each package and subpackage folder to indicate that it is a Python package. Utilize relative import statements such as from .module_name import function_name, where โ€˜.โ€™ represents the current package or moduleโ€™s location. When importing modules from parent directories in Python, one common issue that arises is scope and namespace conflicts.
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Tutor Python
tutorpython.com โ€บ python-import-from-parent-directory
Python Import from parent directory - Tutor Python
October 19, 2023 - Understanding these concepts will ... from the parent directory. Python provides the ability to import functionality from other modules using the import statement. Using this feature, we can access classes, functions, variables, and other resources defined in separate Python files or ...
๐ŸŒ
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.
๐ŸŒ
TraceDynamics
tracedynamics.com โ€บ python-import-from-parent-directory
Python Import from Parent Directory: Module Management
December 8, 2023 - These files can be empty or contain initialization code and indicate to Python that the folder is a package. To import a module from a parent directory, you use the sys.path method, appending the parent directory path to sys.path.
๐ŸŒ
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
๐ŸŒ
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:
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How to do absolute import (a parent folder from the child folder) - Python Help - Discussions on Python.org
July 13, 2021 - The structure of my package is given below package_test main.py __init__.py folder_1 test_1.py __init__.py The main.py contains something from folder_1.test_1 import params if __name__ == "โ€ฆ
๐ŸŒ
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.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ importing modules from parent folder
Importing modules from parent folder | Codemia
January 2, 2025 - If your folders are structured as packages (each folder has an __init__.py file), you can use relative imports: ... This method is more consistent with Python's own recommendations and works especially well when you're developing packages. While modifying sys.path can quickly solve the problem, it's generally better to structure your projects in a way that major dependencies are placed in subdirectories rather than parent directories, or to use package-relative imports.
๐ŸŒ
YouTube
youtube.com โ€บ watch
PYTHON : Import file from parent directory? - YouTube
PYTHON : Import file from parent directory? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Import file from parent di...
Published: December 5, 2021