03-18-2022, 08:43 PM
Python — How to Import Modules From Another Folder?
The most Pythonic way to import a module from another folder is to place an empty file named into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with . The file signals to Python that the folder should be treated as package.
Problem: How to import a file or a module from another folder or directory in Python?
Example: Say, you’ve given the following folder structure:
Your goal is to import functions from in .
Method 1: sys.path.append()
The first method appends the path of the to the system’s path variable.
Note that you need to replace the first three dots in with the concrete path to the folder.
Method 2: sys.path.insert()
A similar alternative is to insert the path of to position 1 of the system’s path variable. This ensures that it’s loaded with higher priority and avoids some naming conflicts:
Again, replace the first three dots in with the concrete path to the folder.
Method 3: Dot Notation with __init__.py
You can also do the following trick—creating a new package.
However, you need to make sure to include an empty file in the directory. This file tells Python to treat the directory as a package. It is considered to be the most Pythonic way of solving this problem.
Method 4: Importlib
A not-so Pythonic alternative is to use the module:
References
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python — How to Import Modules From Another Folder? first appeared on Finxter.
The most Pythonic way to import a module from another folder is to place an empty file named
Code:
__init__.pyCode:
from .. import moduleCode:
__init__.pyProblem: How to import a file or a module from another folder or directory in Python?
Example: Say, you’ve given the following folder structure:
Code:
application ├── app │ └── folder │ └── file_1.py └── app2 └── some_folder └── file_2.pyYour goal is to import functions from
Code:
file_1.pyCode:
file_2.pyMethod 1: sys.path.append()
The first method appends the path of the
Code:
file_1.pyCode:
# file_2.py
import sys
sys.path.append('/.../application/app/folder') import file_1Note that you need to replace the first three dots in
Code:
'/…/application/app/folder'Code:
applicationsMethod 2: sys.path.insert()
A similar alternative is to insert the path of
Code:
file_1.pyCode:
# file_2.py
import sys
sys.path.insert(1, '/.../application/app/folder') import fileAgain, replace the first three dots in
Code:
'/…/application/app/folder'Code:
applicationsMethod 3: Dot Notation with __init__.py
You can also do the following trick—creating a new package.
Code:
# file_2.py
from application.app.folder.file_1 import func_nameHowever, you need to make sure to include an empty
Code:
__init__.pyMethod 4: Importlib
A not-so Pythonic alternative is to use the
Code:
importlibCode:
import importlib.util
spec = importlib.util.spec_from_file_location("file_2", '/.../application/app/folder')
lib = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
lib.function()References
- https://stackoverflow.com/questions/4383...ent-folder
- https://stackoverflow.com/questions/6763...-path?rq=1
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python — How to Import Modules From Another Folder? first appeared on Finxter.

