简体   繁体   English

隐藏包中的实现模块和子模块

[英]Hide implementation modules and submodules in package

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 . public_functions我做进口从一些操作internal_modules.A但不能从internal_modules.B ,都A.pyB.py使用一些功能从C.py

My setup.py is as follows: 我的setup.py如下:

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. 我想使用pip进行安装,但是一旦安装后,我就不想再从包中看到我的internal_modules了。

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. 它会引发ImportError: no module named internal_modules.A public_module.py的第一行中ImportError: no module named internal_modules.A

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. 我知道如果将my_module.internal_modules作为另一个软件包添加到setup.py声明中,则可以解决此问题,但这将使我的internal_modules与已安装软件包中的A.pyB.py公开。

I found a similar question here but it's not working for me 我在这里找到了类似的问题但对我不起作用

You can hide the internals of the module from an import by underscoring the module name: 您可以通过强调模块名称来隐藏导入中的模块内部:

_yourmodulenamegoeshere. _yourmodulename转到此处。

E: You can also define __all__ in the package's __init__ - only the module names from __all__ will be imported via import *. E:你还可以定义__all__在包的__init__ -只有模块名称从__all__将通过进口*进口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM