Note: This answer was intended for a very specific question. For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path.


By default, you can't. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).

However, you can add to the Python path at runtime:

# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')

import file
Answer from Cameron on Stack Overflow
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί importing functions from other folders
r/learnpython on Reddit: Importing functions from other folders
December 27, 2021 -

Hello. I have a code divided over different files like this:

-run_file

folder1

- function_file

folder2

-class_file

I can easily import functions defined in the function_file into the run_file. However I cannot import functions into the class_file because I somehow need to write a code so that the class_file can "look out" of folder2 and look in to folder1. I looked into the code of a person who had a similar project and there were these 'things':

sys.path.append("..")

dirname = os.path.dirname(__file__)

He told me I could do it like this but it did not work for me. Could somebody give me a few hints on how I could achieve this?

Discussions

python - import function from another folder's file - Stack Overflow
This is because it will result in an error known as a circular import. This will cause your script to no longer work. If you want to import a function from docs.py into test.py then put them in the same directory (or directory at the same level of the project hierarchy). More on stackoverflow.com
🌐 stackoverflow.com
Accessing function from .py file in another directory
The term to google is "import from sibling directory". Generally it's a bad idea. You should avoid structuring your code like that. Can you put the main python file in the top level directory? More on reddit.com
🌐 r/learnpython
10
3
July 8, 2022
importing a module from a different folder
Create an empty file called __init__.py inside every folder to turn them into python packages. Then inside the main2.py do this from project.folder1 import main1 Then you can main1.(whatever the name the function is) More on reddit.com
🌐 r/learnpython
8
1
October 9, 2022
Importing python files from different folder
There's a few different ways and talking about the specifics on a Reddit comment gets difficult fast, but if project is a Python package you can do from project.files_2 import file_2 If project is the repository root and files_2 is a package, make sure that it's installed and just do from files_2 import file_2 As a fallback solution you can use sys.path.insert to make Python aware of the files you want to import, but it's not exactly clean and I'd strongly recommend the other solutions instead. If your project is a public repository, I recommend that you post a link here for us to give more specific help. More on reddit.com
🌐 r/learnpython
6
6
September 6, 2023
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-import-module-from-different-directory
Python – Import module from different directory - GeeksforGeeks
August 3, 2026 - # main.py import sys sys.path.insert(0, "../Folder_2") # add Folder_2 path to search list import module1 print(module1.add(5, 3)) # Output: 8 module1.odd_even(10) # Output: Even Β· Explanation: Here, sys.path.insert(0, "../Folder_2") tells Python where to look for module1.py. Instead of modifying code, you can set the PYTHONPATH environment variable, so Python knows where to look.
🌐
iO Flood
ioflood.com β€Ί blog β€Ί python-import-from-another-directory
Python How-To | Import Libraries From Another Directory
February 4, 2024 - # Suppose you have the following directory structure: # # my_project/ # β”œβ”€β”€ main.py # └── my_module/ # └── sub_module.py # You can use a relative import in main.py to import sub_module.py like this: from .my_module import sub_module Β· In this example, the dot (.) before my_module indicates that my_module is in the same directory as the current module (main.py). The imp module provides functions to help find and load Python modules.
🌐
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 ...
🌐
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 - Next, create two Python modules inside the `mypackage` directory: `module1.py` and `module2.py`. ... In this example, the dot (`.`) in the import statement indicates that we want to import `hello()` from a module located in the same package as `module2.py`. To run `module2.py`, navigate to the directory containing the `mypackage` directory and run: ... This will execute the `main()` function inside `module2.py`, which imports and calls `hello()` from `module1.py`.
🌐
MLJAR
mljar.com β€Ί answers β€Ί import-files-from-different-folder-python
Import files from different folder in Python
To import functions from code.py to main.py, you can use following code: ... 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:
Find elsewhere
🌐
GitHub
gist.github.com β€Ί MRobertEvers β€Ί 55a989b4883ea8d7715d2e156627f034
Import Python Files From Another Directory Python 3 Β· GitHub
Since Python will search sys.path when it reaches an import module instruction, we can include the path to that module in sys.path before calling the import. Given a folder structure like this ... This method is inspired by Section 6.4 of the Python docs. It states that the presence of an __init__.py file (may be empty) will cause python to treat the directory it is in as a Package.
🌐
Better Stack
betterstack.com β€Ί community β€Ί questions β€Ί importing-files-from-different-folders-python
Importing files from different folder in Python? | Better Stack Community
You can also use the importlib.import_module() function to import modules from different folders by providing the full path of the module. ... You can also use the from .. import to import module or files from different folders, if the folder ...
🌐
Computer Science Atlas
csatlas.com β€Ί python-import-file-module
Python 3: Import Another Python File as a Module β€” Computer Science Atlas
Let's say we move mymodule.py to a subdirectory called subdir: ... In a file system path, we would separate the components of a path using / (Linux, macOS, etc.) or \ (Windows). In a Python import statement, however, we separate the path components using a dot (.).
🌐
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
🌐
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 - Quick answer: The maintainable way to import Python code from another directory is to define a package, use a qualified import, and run or install it with an explicit project configuration.
🌐
Leapcell
leapcell.io β€Ί blog β€Ί how-to-import-modules-from-another-directory-in-python
How to Import Modules from Another Directory in Python | Leapcell
July 25, 2025 - Python's sys.path is a list that determines the interpreter's search path for modules. By appending the target directory to this list, you can import modules from that directory.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-import-module-outside-directory
Python - Import module outside directory - GeeksforGeeks
July 12, 2025 - This program allows Python to import a module from another directory by adding that directory to the module search path using sys.path.insert(). Python Β· import sys # Add the path of the directory containing mod.py to sys.path sys.path.insert(1, "D:/projects/base/app/modules") # Import the module import mod # Call the hello function from the module mod.hello() Output Β·
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί accessing function from .py file in another directory
r/learnpython on Reddit: Accessing function from .py file in another directory
July 8, 2022 -

Hi,

I have trouble accessing a function that I created in one python file in a directory, by another python file in another directory.

The structure of the files as seen in the screenshot is:

Main Folder:

Folder 1:

file1.py (the function that I want to take is here)

Folder 2:

file2.py (I want to use the function from file1.py)

I saw online a method using sys, this doesn't seem to work. And also the method that you see in the screenshot using the dots - folder1.file1.

How do I access this function? (the intellisence suggests the folder, the file and the function, but one I use it in the code you can see the error in the terminal)

Image:

https://imgur.com/a/SC6W9OR

🌐
YouTube
youtube.com β€Ί savagecamp
Python Import Function From Folder | Python Tutorials | SavageCamp - YouTube
Python Import Function From FolderWe saw how to import from a function from a file when we are on the same folder.But what happens we have to get a function ...
Published: May 23, 2021
Views: 8K
🌐
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...