EDIT Nov 2014 (3 years later):

Python 2.6 and 3.x supports proper relative imports, where you can avoid doing anything hacky. With this method, you know you are getting a relative import rather than an absolute import. The '..' means, go to the directory above me:

from ..Common import Common

As a caveat, this will only work if you run your python as a module, from outside of the package. For example:

python -m Proj

Original hacky way

This method is still commonly used in some situations, where you aren't actually ever 'installing' your package. For example, it's popular with Django users.

You can add Common/ to your sys.path (the list of paths python looks at to import things):

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
import Common

os.path.dirname(__file__) just gives you the directory that your current python file is in, and then we navigate to 'Common/' the directory and import 'Common' the module.

Answer from Dave on Stack Overflow
Top answer
1 of 3
228

EDIT Nov 2014 (3 years later):

Python 2.6 and 3.x supports proper relative imports, where you can avoid doing anything hacky. With this method, you know you are getting a relative import rather than an absolute import. The '..' means, go to the directory above me:

from ..Common import Common

As a caveat, this will only work if you run your python as a module, from outside of the package. For example:

python -m Proj

Original hacky way

This method is still commonly used in some situations, where you aren't actually ever 'installing' your package. For example, it's popular with Django users.

You can add Common/ to your sys.path (the list of paths python looks at to import things):

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
import Common

os.path.dirname(__file__) just gives you the directory that your current python file is in, and then we navigate to 'Common/' the directory and import 'Common' the module.

2 of 3
13

Funny enough, a same problem I just met, and I get this work in following way:

combining with linux command ln , we can make thing a lot simper:

1. cd Proj/Client
2. ln -s ../Common ./

3. cd Proj/Server
4. ln -s ../Common ./

And, now if you want to import some_stuff from file: Proj/Common/Common.py into your file: Proj/Client/Client.py, just like this:

# in Proj/Client/Client.py
from Common.Common import some_stuff

And, the same applies to Proj/Server, Also works for setup.py process, a same question discussed here, hope it helps !

🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί relative importing from another directory. no known parent package.
r/learnpython on Reddit: Relative importing from another directory. No known parent package.
May 8, 2023 -

So i'm trying to import a function from another in order to use it in a different file.

The issue is I'm trying to make it so it's relative because I can't rely on an absolute path whenever this project is build on other computers.

Current hierarchy:

project:
| - packages:
| - - directory1:
| - - - giving_func.py
| - src:
| - - testing:
| - - - taking_func.py

What I'm currently doing inside taking_func.py:

from ...packages.directory1 import giving_func

How can I import the file in another directory without the path error?

Does it make sense to do a relative path instead of an absolute one because this program will be running on different machines?

Top answer
1 of 3
4
Does it make sense to do a relative path instead of an absolute one because this program will be running on different machines? Not really, unless the project structure changes between platforms - which I doubt. I used to struggle with similar problems, using relative imports for all the modules in my packages. However, I came to the realisation that it's better to simply make the package installable and to use absolute imports everywhere, rooted on the parent package. What that means in practice, I have yet to figure out how to explain in the simplest way possible, but perhaps an example repository might help somewhat. The details more or less boil down to Make the project installable (eg. making it possible to run pip install . in the project root to have Python install it as a package). During development you can use an editable installation, meaning changes are immediately reflected, which is useful for unit testing (pip install -e .). Tools like Poetry do this automatically. All this really requires is a basic pyproject.toml file in the repository root directory. Using absolute imports. For example, instead of from ...packages.directory1 import giving_func you might use from project.packages.directory1 import giving_func or something similar instead. If the project has multiple top-level packages, then the specifics depend on that.
2 of 3
1
I second what u/Diapolo10 said above, so I won't bother repeating it, but your issues could be fixed by keeping "packages" within your "src" dir. I tend to do something like this to avoid import problems in my projects repo/ README.md my_package/ ... tests/ ... Nothing stopping you putting that structure within your src/ directory. Some people prefer that. Totally fine.
Discussions

Relative Imports
Hello, I am currently on the topic of Relative Imports. My package is set up as shown in the following figure: I have Windows 11 Home, and Python v3.12. I am attempting to import file_2.py and sub_file.py into file_1.py. The script in my file_1.py is as follows: ''' Desktop\pkg\package1\file_1.py ... More on discuss.python.org
🌐 discuss.python.org
5
1
January 14, 2024
python - Import a module from a relative path - Stack Overflow
Assuming that both your directories ... file inside them), here is a safe solution for inclusion of modules relatively to the location of the script. I assume that you want to do this, because you need to include a set of modules with your script. I use this in production in several products and works in many special scenarios like: scripts called from another directory or executed with python execute instead of opening a new interpreter. import os, sys, inspect ... More on stackoverflow.com
🌐 stackoverflow.com
How can I use relative imports in Python to import a function in another directory - Stack Overflow
I have a directory structure with 2 basic python files inside seperate directories: β”œβ”€β”€ package β”‚ β”œβ”€β”€ subpackage1 β”‚ β”‚ └── module1.py └── subpackage2 └── module2.py module1.py: def More on stackoverflow.com
🌐 stackoverflow.com
python - How do I import from a relative path? - Stack Overflow
You can only import relatively within packages, which means you must add __init__.py files to each subdirectory to make them packages. ... Then you do from ..dirBar import Bar, as discussed in all those duplicate questions. You must also have __init__.py in dirMain and the directory containing dirMain must be on the Python library path... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com β€Ί absolute-vs-relative-python-imports
Absolute vs Relative Imports in Python – Real Python
May 9, 2026 - An absolute import specifies the resource to be imported using its full path from the project’s root folder. Let’s say you have the following directory structure: ... └── project β”œβ”€β”€ package1 β”‚ β”œβ”€β”€ module1.py β”‚ └── module2.py └── package2 β”œβ”€β”€ __init__.py β”œβ”€β”€ module3.py β”œβ”€β”€ module4.py └── subpackage1 └── module5.py Β· There’s a directory, project, which contains two sub-directories, package1 and package2. The package1 directory has two files, module1.py and module2.py.
🌐
Python for the Lab
pythonforthelab.com β€Ί blog β€Ί complete-guide-to-imports-in-python-absolute-relative-and-more
Complete Guide to Imports in Python: Absolute, Relative, and More | Python For The Lab
While importing, we are not specifying a path in the file system, but rather an import path. Therefore, the imports are always relative to the PYTHONPATH, even if called absolute. Another option for importing modules is to define the relative path.
🌐
W3docs
w3docs.com β€Ί python
Import a module from a relative path | W3Docs
Import a module from a relative path in Python using importlib.util Β· For a cleaner approach that avoids modifying sys.path, you can use importlib.util.spec_from_file_location: ... import importlib.util import os # Define the relative path to the module file module_path = os.path.join(os.path.dirname(__file__), 'subdir', 'mymodule.py') # Load the module from the path spec = importlib.util.spec_from_file_location("mymodule", module_path) mymodule = importlib.util.module_from_spec(spec) spec.loader.exec_module(mymodule) # Use the module result = mymodule.some_function()
🌐
Python.org
discuss.python.org β€Ί python help
Relative Imports - Python Help - Discussions on Python.org
Hello, I am currently on the topic of Relative Imports. My package is set up as shown in the following figure: I have Windows 11 Home, and Python v3.12. I am attempting to import file_2.py and sub_file.py into file_1.py. The script in my file_1.py is as follows: ''' Desktop\pkg\package1\file_1.py ''' ''' Action Desired: Import file_2.py and sub_file.py ''' from . import file_2 from .sub_package import sub_file num1 = 11 string1 = "I'm in package1, file_1." def run_1(message)...
Published: January 14, 2024
Top answer
1 of 16
353

Assuming that both your directories are real Python packages (do have the __init__.py file inside them), here is a safe solution for inclusion of modules relatively to the location of the script.

I assume that you want to do this, because you need to include a set of modules with your script. I use this in production in several products and works in many special scenarios like: scripts called from another directory or executed with python execute instead of opening a new interpreter.

 import os, sys, inspect
 # realpath() will make your script run, even if you symlink it :)
 cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
 if cmd_folder not in sys.path:
     sys.path.insert(0, cmd_folder)

 # Use this if you want to include modules from a subfolder
 cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
 if cmd_subfolder not in sys.path:
     sys.path.insert(0, cmd_subfolder)

 # Info:
 # cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
 # __file__ fails if the script is called in different ways on Windows.
 # __file__ fails if someone does os.chdir() before.
 # sys.argv[0] also fails, because it doesn't not always contains the path.

As a bonus, this approach does let you force Python to use your module instead of the ones installed on the system.

Warning! I don't really know what is happening when current module is inside an egg file. It probably fails too.

2 of 16
335

Be sure that dirBar has the __init__.py file -- this makes a directory into a Python package.

Find elsewhere
🌐
Python Forum
python-forum.io β€Ί thread-42513.html
import a function from another file using relative path
July 31, 2024 - Hi It's a naive question to find the correct synthax (all i tested failed): Context: In MyFile1.py, i want to load the function1 and function2. If all files are located in the current directory (traditional situation): from MyFile1 import function...
🌐
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
🌐
Gideonbrimleaf
gideonbrimleaf.github.io β€Ί 2021 β€Ί 01 β€Ί 26 β€Ί relative-imports-python.html
Relative Imports in Python - Without Tearing Your Hair Out | Aiming to Misbehave
January 26, 2021 - To get the test file running regardless of where we are running Python from we need to append the relative parent directory to Python’s in-built sys.path variable.
🌐
CodeRivers
coderivers.org β€Ί blog β€Ί python-import-file-from-another-directory
Python: Importing Files from Another Directory - CodeRivers
August 8, 2026 - Yes β€” if the file is inside a package that is installed (either from PyPI or via pip install -e .), you can import it directly. You can also use relative imports within a package, as long as you run the code as a module (python -m package.module). While __init__.py is technically optional ...
🌐
YouTube
youtube.com β€Ί black lotus
Python How To Import Relative Path - YouTube
An explanation how to import modules and functions from relative paths.The method used makes use of the SourceFileLoader from the importlib library.
Published: January 16, 2022
Views: 2K
🌐
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:
🌐
Excendra
excendra.com β€Ί home β€Ί blogs β€Ί python import from another directory: the ultimate developer guide
Python Import From Another Directory Complete Guide
February 12, 2026 - If you are searching for how python import from another directory works, the simple answer is that Python lets you import files from outside your current folder by managing its import search path, using environment variables, and structuring your project correctly with packages and relative imports.
Top answer
1 of 3
4

To be considered a package, a Python directory has to include an __init__.py file. Since your module2.py file is not below a directory that contains an __init__.py file, it isn't considered to be part of a package. Relative imports only work inside packages.

UPDATE:

I only gave part of the answer you needed. Sorry about that. This business of running a file inside a package as a script is a bit of a can of worms. It's discussed pretty well in this SO question:

Relative imports in Python 3

The main take-away is that you're better off (and you're doing what Guido wants you to) if you don't do this at all, but rather move directly executable code outside of any module. You can usually do this by adding an extra file next to your package root dir that just imports the module you want to run.

Here's how to do that with your setup:

.
β”œβ”€β”€ package
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ subpackage1
β”‚   β”‚   └── module1.py
β”‚   └── subpackage2
β”‚       └── module2.py
└── test.py

test.py:

import package.subpackage2.module2

You then run test.py directly. Because the directory containing the executed script is included in sys.path, this will work regardless of what the working directory is when you run the script.

You can also do basically this same thing without changing any code (you don't need test.py) by running the "script" as a module.

python3 -m package.subpackage2.module2

If you have to make what you're trying to do work, I think I'd take this approach:

import os, sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from subpackage1.module1 import module1

module1()

So you compute in a relative way where the root of the enclosing package is in the filesystem, you add that to the Python path, and then you use an absolute import rather than a relative import.

There are other solutions that involve extra tools and/or installation steps. I can't think why you could possibly prefer those solutions to the last solution I show.

2 of 3
1

By default, Python just considers a directory with code in it to be a directory with code in it, not a package/subpackage. In order to make it into a package, you'll need to add an __init__.py file to each one, as well as an __init__.py file to within the main package directory.

🌐
Pierian Training
pieriantraining.com β€Ί home β€Ί importing a module from a different directory in python
Importing a Module from a Different Directory in Python - Pierian Training
April 28, 2023 - In this case, you need to specify ... or relative path. An absolute path specifies the full path to the module on your file system. For example: import sys sys.path.append('/path/to/other/directory') import my_module Β· This adds the directory containing `my_module` to the list of directories where Python searches for ...
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 19650531 β€Ί how-do-i-import-from-a-relative-path
python - How do I import from a relative path? - Stack Overflow
You can only import relatively within packages, which means you must add __init__.py files to each subdirectory to make them packages. ... Then you do from ..dirBar import Bar, as discussed in all those duplicate questions.
🌐
Finxter
blog.finxter.com β€Ί home β€Ί learn python blog β€Ί python β€” how to import modules from another folder?
Python - How to Import Modules From Another Folder? - Be on the Right Side of Change
July 18, 2023 - The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from ...
🌐
RunxBuild
runxbuild.com β€Ί home β€Ί blog β€Ί python import from another directory: why it fails and what to do
Python Import From Another Directory: Why It Fails and What to Do
August 10, 2026 - Use python -m package.module instead. ... Install the package with pip install -e . and imports just work with no configuration. If tests need sys.path manipulation in conftest.py, the packaging is wrong and the same problem will reappear in ...
🌐
Python Pool
pythonpool.com β€Ί home β€Ί tutorials β€Ί import python from another directory: packages and project layout
Import Python from Another Directory: Packages and Project Layout
July 13, 2026 - When a module moves, the import path tells you exactly which package boundary is involved. For a small script, you can add a known directory to sys.path. Use an absolute path built from the current file, not a fragile relative path.