'How to avoid importing module private functions?

I am asking about module privates, not class privates.

Suggested in here, a module private starts with one underscore, such a element is not copied along when using the from <module_name> import * form of the import command; it is however imported if using the import <moudule_name> syntax.

So the question is if I have to use import <moudule_name> syntax, how to avoid importing module private functions?



Solution 1:[1]

A feasible way is to create a separate folder for the module, add __init__.py file and import all the contents of the module.

We assume that the original structure is as follows:

.
??? module_a.py
??? test_module_a.py

Modify it to:

.
??? module_a
?   ??? __init__.py
?   ??? _module_a.py
??? test_module_a.py

The __init__.py file only imports the contents of the module a file using the asterisk syntax:

# module_a/__init__.py
from ._module_a import *

So when you are importing the module_a, the private function will not be visible because it has not been imported by the __init__.py file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mechanic Pig