0

Let's say we have an app, app.py, that does a variety of things. Of course, along the way, there is bound to be "helper" functions that do things such as cleaning data, creating a visualization, doing currency conversion, etc etc. I've learned that storing these helper functions elsewhere is helpful in decluttering the main script, increasing readability. My question regards the most pythonic way to import these helper functions to the main script.

The way I've been doing it is the following:

app_dir
|
|
|----- helper_modules
|       |_ __init__.py
|       |_ module1.py
|       |_ module2.py
|       |_ module3.py
|
|----- app.py

And importing them the following way in app.py:

from helper_modules.module1 import _function(s)_
from helper_modules.module2 import _function(s)_
from helper_modules.module3 import _function(s)_

And everything works fine. However, I've seen source code that does it the following way (example here):

app_dir
|
|
|----- utils
|       |_ module1.py
|       |_ module2.py
|       |_ module3.py
|
|----- app.py

And import the scripts the following way in app.py:

from utils import module1, module2, module3

Ultimately, my question is: What is the most pythonic way to import custom scripts into the main file? Do you want to turn the folder of helper functions into a module by including the __init__.py file or is just creating a utils folder fine? Also, instead of helper_modules in the first example, should it be renamed to utils (that is, is that a conventional naming rule)? I want to make sure my directory structure is as pythonic and readable as possible as others will be reading it.

4
  • The only answer is "it depends". Sometimes, it's more useful to import the module and access the functions as attributes of that module to make it clear where the functions are defined. Other times, the function name alone is sufficient to allow it to be imported directly. Commented Sep 3, 2022 at 16:51
  • IMO, it's often better to err on the side being explicit and importing the module rather than the function name. Commented Sep 3, 2022 at 16:52
  • So you're saying you prefer the first example, but without specifying the function name, as in from helper_modules import module3 then in the main script utilizing the function with module3.function(...) @chepner? Commented Sep 3, 2022 at 16:55
  • Are you asking about the package name used? I don't consider that part of the question at all. The second example makes it clearer where module1.foo comes from compared to a bare name foo being added to the global namespace. Commented Sep 3, 2022 at 16:58

1 Answer 1

0

Many ways work, however to make later refactoring easier I use:

In __init__.py

from helper_modules.module1 import myfunction

__all__ = ['myfunction']

In app.py

from helper_modules import myfunction

This makes moving my_function to module2 later easier.

Docs for __all__.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.