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
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-import-module-from-different-directory
Python – Import module from different directory - GeeksforGeeks
August 3, 2026 - This tells Python: "This folder is a package, and its files can be imported." ... project/ β”œβ”€β”€ Folder_1/ β”‚ β”œβ”€β”€ __init__.py β”‚ └── main.py └── Folder_2/ β”œβ”€β”€ __init__.py └── module1.py ... from Folder_2 import module1 print(module1.add(5, 15)) # Output: ...
Discussions

python - How to import other scripts in the same folder? - Geographic Information Systems Stack Exchange
I'm writing a PyQGIS script and I would import in my script other external scripts. I tried in this way but the code doesn't work. from PyQt5.QtGui import * from PyQt5.QtCore import * import More on gis.stackexchange.com
🌐 gis.stackexchange.com
How to import python file from folder as folder.file using "from" keyword?
I had a file at db/connection.py. I moved the file and now the path is project/db/connection.py. Previously I imported that file using this syntax: import db.connection And somewhere in the code I had access to the fi… More on discuss.python.org
🌐 discuss.python.org
1
0
October 30, 2022
Import files directly from file location - Ideas - Discussions on Python.org
Importing new modules in Python is a pain, and far from straightforward. The trivial way to import a new module would be to give the full path of the file containing the required code - which could be a python __init__ module itself, setting up a fully functional module. More on discuss.python.org
🌐 discuss.python.org
0
July 25, 2023
How to import python files present in a folder(within a container) in another python file?
Inside the folder "ABC", I have another python file called "xyz.py". I want to import xyz.py in main.py. ... An Azure service that offers file shares in the cloud. ... Globally unique resources that provide access to data management services and serve as the parent namespace for the services. ... Could you please elaborate a bit more on your scenario, what you are trying to do, where are you executing the python file from... More on learn.microsoft.com
🌐 learn.microsoft.com
0
0
May 30, 2023
🌐
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
🌐
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: application β”œβ”€β”€ main.py β”œβ”€β”€ __init__.py β”œβ”€β”€ app β”‚ β”œβ”€β”€ ...
🌐
GitHub
gist.github.com β€Ί MRobertEvers β€Ί 55a989b4883ea8d7715d2e156627f034
Import Python Files From Another Directory Python 3 Β· GitHub
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. That means we will be able to import <directory> as a module. Notice that one of the directories in sys.path by default is the directory that the python file launched from...
Find elsewhere
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί reference β€Ί import.html
5. The import system β€” Python 3.15.2 documentation
Python’s default sys.meta_path has three meta path finders, one that knows how to import built-in modules, one that knows how to import frozen modules, and one that knows how to import modules from an import path (i.e.
🌐
Better Stack
betterstack.com β€Ί community β€Ί questions β€Ί importing-files-from-different-folders-python
Importing files from different folder in Python? | Better Stack Community
Alternatively, you can use the PYTHONPATH environment variable to add the path to the folder containing the file you want to import. You can also use the importlib.import_module() function to import modules from different folders by providing ...
🌐
Medium
medium.com β€Ί @vamsik23 β€Ί import-local-python-modules-a6e74950de4a
Import Local Python Modules. We all use modules in most of our… | by vamsi | Medium
August 18, 2021 - Here we can see that I am adding folder_1 to python path using sys.path.append function so when I ask python to import some modules then it will check in folder_1 too. Please keep in mind to add whole path to the folder, don’t use β€œ~” in the append path.
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί modules.html
6. Modules β€” Python 3.15.1 documentation
When importing the package, Python searches through the directories on sys.path looking for the package subdirectory. The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). This prevents directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
🌐
Python.org
discuss.python.org β€Ί python help
How to import python file from folder as folder.file using "from" keyword? - Python Help - Discussions on Python.org
October 30, 2022 - I had a file at db/connection.py. I moved the file and now the path is project/db/connection.py. Previously I imported that file using this syntax: import db.connection And somewhere in the code I had access to the file: db.connection.open_connection() Since now the file is moved, I know only two options to import the file: using import and from import keywords.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-import-from-file-importing-local-files-in-python
Python Import from File – Importing Local Files in Python
July 6, 2023 - Here are the key concepts and techniques for handling file paths and directories when importing local files in Python: A file path is a string that represents the location of a file or directory in the file system. An absolute path specifies the complete path starting from the root directory.
🌐
Quora
quora.com β€Ί How-do-I-import-a-py-file-into-another-folder
How to import a .py file into another folder - Quora
Answer (1 of 3): An alternate solution to that suggested by David Mankins would be to add the path of the Python file you want to import to the PYTHONPATH environment variable. I recommend this solution only if you have a folder/directory where you want to keep all your own Python libraries, whic...
🌐
Python.org
discuss.python.org β€Ί ideas
Import files directly from file location - Ideas - Discussions on Python.org
July 25, 2023 - Importing new modules in Python is a pain, and far from straightforward. The trivial way to import a new module would be to give the full path of the file containing the required code - which could be a python __init__ module itself, setting up a fully functional module.
🌐
Pythonhow
pythonhow.com β€Ί what β€Ί what-to-do-to-import-files-from-different-folder-in-python
What to do to import files from a different folder in python
You can also use the os.path.abspath() function to get the absolute path to the directory containing file1.py, and then use that path to add the directory to the Python path. For example: import sys import os path = os.path.abspath("module1") sys.path.append(path) import file1You can also use the PYTHONPATH environment variable to specify additional directories that Python should search for modules and packages.
🌐
Computer Science Atlas
csatlas.com β€Ί python-import-file-module
Python 3: Import Another Python File as a Module β€” Computer Science Atlas
The name used in the import statement is simply the module's filename without the .py extension at the end. Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory.
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί answers β€Ί questions β€Ί 1294283 β€Ί how-to-import-python-files-present-in-a-folder-wit
How to import python files present in a folder(within a container) in another python file? - Microsoft Q&A
May 30, 2023 - I have a container named "input". Inside this container, I have a python file called "main.py" and a folder "ABC". Inside the folder "ABC", I have another python file called "xyz.py". I want to import…
🌐
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__ == "…
🌐
Spark By {Examples}
sparkbyexamples.com β€Ί home β€Ί python β€Ί import files from different folder in python
Import Files from Different Folder in Python - Spark By {Examples}
May 31, 2024 - How to Import files from Different Folder in Python? In this article, we will learn several methods for importing files from different folders, including