Home > Back-end >  How to import two python files as a single module in Python?
How to import two python files as a single module in Python?

Time:10-16

Im working in a Python project with several functionalities. Each of these functionalities works as its own project with its own modules.

I have defined an utils_general.py module with general functions that can be used by each functionality. In addition, each functionality has its own utils_functionality_x.py module with functions that will be used only in that module. The structure of my project is as follows:

.
 -- __init__.py
 -- src
|    -- utils_general.py
|    -- functionality_1.py
|   |    -- __init__.py
|   |    -- main_functionality_1.py
|   |    -- utils_functionality_1.py
|    -- functionality_2.py
|   |    -- __init__.py
|   |    -- main_functionality_2.py
|   |    -- utils_functionality_2.py
 -- .gitignore
 -- README.md

In the above example, in main_functionality_1.py it will be used functions from utils_general.py and from utils_functionality_1.py. As well, in main_functionality_2.py it will be used functions from utils_general.py and from utils_functionality_2.py.

I would like to import all the functions of the utils' files as if it was a single module. With this I mean, I would like to make utils_functionality_1.py "inherit" the functions of utils_general.py. The idea is something similar to class-inheritance, but the two files are completelly functional (in utils_general.py and utils_functionality_1.py there are no classes defined, only functions).

For example, this will result that in main_functionality_1.py I could access to the functions of utils_general.py and utils_functionality_1.py with the name utils (notice that no functions have the same name in utils_general.py and utils_functionality_1.py). For example, I would like to do something like this (this won't work):

# This is pseudo-code and won't work
import src.utils_general as utils
import src.functionality_1.utils_functionality_1 as utils

All I can think of is importing the functions of utils_general.py into utils_functionality_1.py and then creating a wrapper function that can be called from outside. Something like this:

Code in utils_functionality_1.py:

# Code in utils_functionality_1.py
import src.utils_general as utils_general

def function_general(*args, **kwargs):
    return utils_general.function_general(*args, **kwargs)

Is there a better way/good-practice to do this "pseudo-inheritance" between modules in Python?

CodePudding user response:

You have to import all the names from utils_general in utils_functionality_1 and that's it.

# utils_functionality_1.py
from utils_general import *

CodePudding user response:

You may have to change your folder structure a bit, but you might be able to use "namespace packages" to extend an existing module with additional files.

Namespace packages are a mechanism for splitting a single Python package across multiple directories on disk.

Here's and answer I wrote on the topic.

  • Related