Importing Files from Different Folders in Python

Introduction

When working on larger Python projects, it is common to organize your code into different folders and files for better maintainability and organization. However, when you have multiple folders and files, you may encounter a situation where you need to import a function or module from one file to another file in a different folder.

Understanding the Problem

Let's consider the following folder structure:

application
├── app
│   └── folder
│       └── file.py
└── app2
    └── some_folder
        └── some_file.py
        

In this structure, we have a file file.py located in the application/app/folder directory, and another file some_file.py located in the application/app2/some_folder directory. Now, let's say we want to import a function called func_name from file.py into some_file.py.

Solution

There are a few ways to solve this issue and import files from different folders:

1. Using the sys.path.append method

The sys.path variable contains a list of directory names where Python looks for modules and packages. By appending the parent directory of the file you want to import to sys.path, you can import the desired file.

import sys
sys.path.append('../../app/folder')  # relative path to the desired file
from file import func_name

In this example, we've appended the relative path from some_file.py to file.py to the sys.path list. Now, we can import the func_name function using from file import func_name.

2. Using the __init__.py file

In Python, a directory is considered a package if it contains an __init__.py file. This file can be empty or contain initialization code for the package. By creating an __init__.py file in each of the folders, we can import files across folders using relative imports.

In the application/app/folder directory, create an empty __init__.py file. Similarly, create an empty __init__.py file in the application/app2/some_folder directory. Now, you can import the func_name function using relative imports.

In some_file.py:

from ..folder.file import func_name

In this example, we use the .. notation to indicate that we want to go up one level in the file hierarchy before accessing the folder.file module.

3. Using the PYTHONPATH environment variable

The PYTHONPATH environment variable is another option to import files from different folders. By adding the parent directory of the file you want to import to this variable, you can import the desired file.

export PYTHONPATH="../../app/folder"  # for Unix-based systems
set PYTHONPATH=..\..\app\folder  # for Windows systems

from file import func_name

By adding ../../app/folder to the PYTHONPATH on Unix-based systems or ..\..\app\folder on Windows systems, Python will be able to locate and import the func_name function.

Conclusion

When working with complex Python projects with multiple folders and files, it's important to understand how to import files from different folders. In this article, we discussed three different methods:

  • Using the sys.path.append method
  • Using the __init__.py file
  • Using the PYTHONPATH environment variable

By following these methods, you can import files or modules from different folders in Python and organize your code more efficiently.