You are currently viewing Import Files from Different Folder in Python

How to Import files from Different Folder in Python? In this article, we will learn several methods for importing files from different folders, including using relative imports, adding directories to sys.path, and using absolute imports with package names.

Advertisements

In a Python project, you might have multiple files and directories that contain code and data. Sometimes, you may need to import these files from a different folder to use a function or variable defined in that file.

1. Relative Imports

We can use relative imports to import modules from packages that are in the same directory or a subdirectory of the current module. Relative imports are specified using the . (dot) notation to indicate the position of the module relative to the current module.

Relative imports can be useful when we have a large project with many modules and packages, and we want to organize our code into a logical directory structure.

Remember the following file structure. We will use it throughout this article. Let’s say we have a project with the following folders and files.


project/
├── main.py
├── utils/
│   ├── __init__.py
│   └── file.py
└── data/
    ├── __init__.py
    └── data.csv

Let’s suppose, the main.py file that needs to import a function from the file.py module in the utils package.

To import the data.py to main.py using a relative import, see the following code.


# File content from main.py

# Import 
from data.data import load_data

data = load_data()
print(data)

The data.py files contain the following code. We could import the entire file, which we will see in upcoming section. For now, we have imported the function from that file using relative import.


# File content of data/data.py

import csv

def load_data():
    with open('data/data.csv', 'r') as f:
        reader = csv.reader(f)
        data = list(reader)
    return data

We will get an ImportError, If we try to use a relative import to import a file from a different package. That is why, when we use a relative import, the file we are importing from must be in the same package or a subpackage of the current package.

2. Absolute Imports with sys.path

If we have a complex directory structure using relative imports may not be feasible, we can use absolute imports with sys.path to specify the file path to the module we want to import.

sys.path is a list of directories where Python looks for modules when we import them. By adding the path to the directory containing the module we want to import to sys.path, we can use absolute imports to import the module from anywhere in our project.

Suppose we want to import the my_function function from the file.py module in the utils package into main.py. To do this with absolute imports, we can add the path to the project directory to sys.path, like this:


# main.py

import sys
sys.path.append('/path/to/project')

from utils.file import my_function

my_function()

The path we append to sys.path should be the path to the directory containing the top-level package of our project. We can then use absolute imports to import modules from anywhere in our project.

Here’s an example of using absolute imports with sys.path to access the data.csv file from the file.py module:


# utils/file.py

import sys
import os
import csv

# Get the path to the data directory relative to this module
data_path = os.path.join(os.path.dirname(__file__), '..', 'data')

# Add the data directory to sys.path
sys.path.append(data_path)

# Import the data.csv file
from data import data

with open(data, 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

3. Absolute Imports with Package Name

Another way to use absolute imports is by specifying the full package name of the module you want to import. In this case, Python will start looking for the module from the top-level directory of your project.

To import the a variable from file.py into main.py using absolute imports with the package name, we would use the following syntax:


from project.utils.file import a

print(a)

This method will work if the top-level directory of the project must be in the PYTHONPATH environment variable. We can add it programmatically using sys.path.

4. Importing a Module as an Object

Another way to import a module or a file from a different folder in Python is to import it as an object. This method can be useful if we want to access the attributes and methods of a module or a file using dot notation.


import utils.file as f

print(f.a)

5. Summary and Conclusion

We have explained several ways to import modules or files from a different folder in Python. You can use relative imports, absolute imports with sys.path, absolute imports with package name, or importing a module as an object. I hope this article was helpful, if you have any questions, leave them in the comment section.

Happy Coding!