python pypi package module visibility

前端 未结 1 1377
感动是毒
感动是毒 2021-01-21 23:45

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!

Actually, I ju

相关标签:
1条回答
  • 2021-01-22 00:14

    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.

    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).

    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.

    0 讨论(0)
提交回复
热议问题