问题
I want to package my code to expose only the main functions. My directory is like this:
./
setup.py
my_module/
__init__.py
public_functions.py
internal_modules/
__init__.py
A.py
B.py
other_modules.py/
__init__.py
C.py
In public_functions I do import some operations from internal_modules.A but NOT from internal_modules.B, and both A.py and B.py uses some functions from C.py.
My setup.py is as follows:
from setuptools import setup
setup(name='my_module',
version='0.1',
description='my_awesome_module',
author='Me',
author_email='example@mail.com',
license='MIT',
packages=['my_module'],
zip_safe=False)
I want to install it with pip, but I want not to let any of my internal_modules visible from my package once it is installed.
I could install it properly but when I do
from my_module import public_module
it throws ImportError: no module named internal_modules.A in public_module.py's first line.
I know I can fix it if I add my_module.internal_modules to my setup.py declaration as another package, but this will let my internal_modules public with A.py and B.py visible from installed package.
I found a similar question here but it's not working for me
回答1:
You can hide the internals of the module from an import by underscoring the module name:
_yourmodulenamegoeshere.
E:
You can also define __all__ in the package's __init__ - only the module names from __all__ will be imported via import *.
来源:https://stackoverflow.com/questions/47224965/hide-implementation-modules-and-submodules-in-package