NeuroAgent

How to Import Functions Between Folders in Python

Learn the correct ways to import functions from different directories in Python. Fix ModuleNotFoundError with proper package structure and sys.path modifications.

Question

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:

python
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?

NeuroAgent

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

Python’s import system works by searching for modules in directories listed in sys.path. This typically includes:

  1. The directory containing the script (current working directory)
  2. Directories listed in the PYTHONPATH environment variable
  3. Standard library directories
  4. 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:

python
# 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:

python
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:

python
# 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:

bash
# 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:

python
from application.app.folder.file import func_name

Best Practices

  1. Always use __init__.py files to make directories proper Python packages
  2. Prefer relative imports when modules are within the same package
  3. Use absolute imports when importing from different packages
  4. Avoid modifying sys.path in production code - use proper package structure instead
  5. 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

  1. Python Documentation - The import system
  2. Real Python - Python Modules and Packages
  3. GeeksforGeeks - Python import sys
  4. Stack Overflow - How to import a module from parent directory

Conclusion

To successfully import a function from a different folder in Python:

  1. Add __init__.py files to all your directories to make them proper packages
  2. Use absolute imports with the full package path: from application.app.folder.file import func_name
  3. Ensure the parent directory of application is in Python’s import path through sys.path, PYTHONPATH, or by running from the correct directory
  4. For development, modifying sys.path is 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.