问题描述
我刚刚发布了一个 PyPi 包 但是在我 pip install 之后
- 自己编辑,我发现有很多可见的模块不应该!
I've just published a PyPi package but after I pip install
-ed it myself, I found out that there are many visible modules that shouldn't be!
实际上,我只想让两个函数可见(super_clean
和 simple_clean
),并且它们都在 gutenberg_cleaner.py
中.
Actually, I just want two functions to be visible (super_clean
and simple_clean
), and both of them are in gutenberg_cleaner.py
.
我不知道如何让其他模块和功能对用户隐藏.
I don't know how to make other modules and function hidden to the user.
任何帮助将不胜感激.
如果有用,这是我的setup.py
:
from setuptools import setup
with open('README.md') as f:
long_description = f.read()
setup(
name="gutenberg_cleaner",
install_requires=['nltk'],
version='0.1.0',
description="cleans gutenberg dataset books",
author_email='[email protected]',
py_modules=["gutenberg_cleaner"],
url="https://github.com/kiasar/gutenberg_cleaner",
license='MIT',
long_description=long_description,
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Operating System :: OS Independent",
],
)
推荐答案
一般来说,没有办法在 Python 中隐藏"给定的变量/函数/类/模块.一切都可以导入给用户,甚至是标准库中的东西.
In general, there isn't a way to "hide" a given variable/function/class/module in Python. Everything is available for importing to the user, even things in the standard library.
在实践中,在 Python 中使用下划线作为不属于公共 API 的一部分的前缀是惯用的,例如:
In practice, it is idiomatic in Python to prefix something that is not part of a public API with an underscore, such as:
from gutenberg_cleaner import _internal_helper_method
这向知道此习语的用户表明这不是要导入的".
This indicates to your users who are aware of this idiom that "this is not meant to be imported".
这实际上并没有阻止用户导入这个内部函数,但是对于大多数项目来说,这已经足够了(我认为这是你应该在这里做的).
This doesn't actually prevent the user from importing this internal function, but for most projects, this is sufficient (and I think this is what you should do here).
也就是说,还有另一种选择:有一个第三方库 publication
这正是你想要的:你定义了一个可以导入的函数列表,库阻止了其余的被导入.它没有被广泛使用,但确实可以解决您的问题.
That said, there is another option: there is a third-party library publication
that does precisely what you want: you define a list of functions that can be imported, and the library prevents the rest from being imported. It's not widely used, but it does resolve your question.
这篇关于python pypi包模块可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!