How to import a function from a different folder in Python?
I have the following folder structure:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
How can I import a function from file.py into some_file.py? I tried using:
from application.app.folder.file import func_name
but it doesn’t work. What is the correct way to import a function from a different directory in Python?
To import a function from a different folder in Python, you need to understand how Python’s import system works with directory structures. The issue with your approach is likely related to missing __init__.py files or Python’s inability to find the application directory in its import path.
Contents
- Understanding Python Import System
- Solution 1: Using Relative Imports
- Solution 2: Using Absolute Imports with Package Structure
- Solution 3: Modifying sys.path
- Solution 4: Using PYTHONPATH Environment Variable
- Best Practices
- Common Issues and Troubleshooting
Understanding Python Import System
Python’s import system works by searching for modules in directories listed in sys.path. This typically includes:
- The directory containing the script (current working directory)
- Directories listed in the
PYTHONPATHenvironment variable - Standard library directories
- Site-packages directories from installed packages
For your import to work, Python needs to recognize the application directory as part of its search path.
Solution 1: Using Relative Imports
If some_file.py is trying to import from file.py, you can use relative imports:
# In some_file.py
from ...app.folder.file import func_name
However, relative imports only work when running the module as part of a package, not when running it directly as a script.
Solution 2: Using Absolute Imports with Package Structure
The most reliable method is to make your directories proper Python packages by adding __init__.py files:
application/
├── __init__.py # Add this file
├── app/
│ ├── __init__.py # Add this file
│ └── folder/
│ ├── __init__.py # Add this file
│ └── file.py
└── app2/
├── __init__.py # Add this file
└── some_folder/
├── __init__.py # Add this file
└── some_file.py
Then in some_file.py, you can use:
from application.app.folder.file import func_name
To make this work, you need to run your script from the parent directory of application or add the parent directory to sys.path.
Solution 3: Modifying sys.path
You can dynamically add the parent directory to Python’s import path:
# In some_file.py
import sys
import os
# Add the parent directory to sys.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from application.app.folder.file import func_name
This approach adds two levels up to sys.path, making application importable.
Solution 4: Using PYTHONPATH Environment Variable
You can set the PYTHONPATH environment variable to include the parent directory of application:
# In terminal (Linux/macOS)
export PYTHONPATH="/path/to/parent/directory:$PYTHONPATH"
# In terminal (Windows)
set PYTHONPATH="C:\path\to\parent\directory;%PYTHONPATH%"
Then your import statement will work as written:
from application.app.folder.file import func_name
Best Practices
- Always use
__init__.pyfiles to make directories proper Python packages - Prefer relative imports when modules are within the same package
- Use absolute imports when importing from different packages
- Avoid modifying
sys.pathin production code - use proper package structure instead - Consider using virtual environments to manage project dependencies
Common Issues and Troubleshooting
Issue: ModuleNotFoundError: No module named 'application'
Solution: Add __init__.py files and ensure the parent directory is in sys.path
Issue: ImportError: attempted relative import with no known parent package
Solution: Don’t run the script directly - use python -m application.app2.some_folder.some_file from the parent directory
Issue: Circular imports
Solution: Restructure your code to avoid circular dependencies or use lazy imports
Sources
- Python Documentation - The import system
- Real Python - Python Modules and Packages
- GeeksforGeeks - Python import sys
- Stack Overflow - How to import a module from parent directory
Conclusion
To successfully import a function from a different folder in Python:
- Add
__init__.pyfiles to all your directories to make them proper packages - Use absolute imports with the full package path:
from application.app.folder.file import func_name - Ensure the parent directory of
applicationis in Python’s import path throughsys.path,PYTHONPATH, or by running from the correct directory - For development, modifying
sys.pathis acceptable, but for production code, use proper package structure and installation methods
The most robust solution is to restructure your project with proper __init__.py files and install your package in development mode using pip install -e . from the parent directory.