2

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?

IgnacioGaBo
  • 168
  • 11
  • You can do 'from package_name.src.utils_general import * ' in utils_functionality_1 if your main_functionality_1 is being called from the package and can resolve the import. – kubatucka Oct 15 '21 at 18:31

2 Answers2

1

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 *
nadapez
  • 2,603
  • 2
  • 20
  • 26
0

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.

Chris
  • 6,805
  • 3
  • 35
  • 50