简体   繁体   English

Python包导入子包-好的做法?

[英]Python package import subpackage - good practice?

My package has a dependency on the latest version of the jsonpickle package. 我的软件包依赖于最新版本的jsonpickle软件包。 Older versions are installable via pip, however I need the latest version (ie on Github) for it to work. 较旧的版本可以通过pip安装,但是我需要最新的版本(例如,在Github上)才能运行。 Is it generally considered OK in this situation to bundle the latest version of jsonpickle in my code? 在这种情况下,将最新版本的jsonpickle捆绑在我的代码中通常被认为可以吗? Is there some other solution? 还有其他解决方案吗? I'd rather not ask my users not to clone from github. 我不想让我的用户不要从github克隆。

I'm thinking of organising my package like this: 我正在考虑这样组织我的包裹:

My package
    |
__init__.py
 file1.py
 file2.py
          \
        jsonpickle (latest)

ie Doing what was done here: Python: importing a sub‑package or sub‑module 即执行此处的操作: Python:导入子包或子模块

As kag says, this is generally not a good idea. 正如kag所说,这通常不是一个好主意。 It's not that it's "frowned upon" as being unfriendly to the other packages, it's that it causes maintenance burdens for you and your users. 这并不是说它不赞成其他软件包,而是因为它给您和您的用户带来了维护负担。 (Imagine that there's a bug that's fixed in jsonpickle that affects your users, but you haven't picked up the fix yet. If you'd done things normally, all they'd have to do is upgrade jsonpickle , but if you're using an internal copy, they have to download the jsonpickle source and yours, hack up your package, and install it all manually.) (想像一下, jsonpickle已修复了一个会影响用户的错误,但您尚未修复此错误。如果您正常进行操作,那么他们要做的就是升级jsonpickle ,但是如果使用内部副本,他们必须下载jsonpickle源和您jsonpickle源,破解您的软件包,然后手动安装它们。)

Sometimes, it's still worth doing. 有时,还是值得做的。 For example, the very popular requests module includes its own copy of other packages like urllib3 . 例如,非常受欢迎的requests模块包括它自己的其他包的副本 ,例如urllib3 And yes, it does face both of the costs described above. 是的,它确实要面对上述两个成本。 But it also means that each version of request can rely on an exact specific version of urllib3 . 但这也意味着每个版本的request都可以依赖于urllib3特定版本。 Since requests makes heavy use of urllib3 's rarely-used interfaces, and even has workarounds for some of its known bugs, that can be valuable. 由于requests大量使用了urllib3很少使用的接口,甚至还解决了一些已知错误,因此这可能很有价值。

In your case, that doesn't sound like the issue. 就您而言,这听起来不像是问题。 You just need a bleeding-edge version of jsonpickle temporarily, until the upstream maintainers upload a new version to PyPI. 您只需要临时使用最新版本的jsonpickle ,直到上游维护者将新版本上传到PyPI。 The problem isn't that you don't want your users all having different versions; 问题不在于您不希望所有用户都使用不同的版本; it's that you don't want to force them to clone the repo and figure out how to install it manually. 这是您不想强迫他们克隆存储库并弄清楚如何手动安装它。 Fortunately, pip takes care of that for you, by wrapping most of the difficulties up in one line: 幸运的是, pip通过将大多数困难汇总为一行来为您解决这一问题:

pip install git+https://github.com/foo/bar

It's not a beautiful solution, but it's only temporary, right? 这不是一个漂亮的解决方案,但这只是暂时的,对吧?

It's generally not the best idea to bundle some dependency with your project. 通常,将某些依赖项与项目捆绑在一起并不是最好的主意。 Some projects do it anyway, or bundle it as an alternative if there's no system package available. 有些项目还是这样做,或者在没有可用的系统软件包的情况下将其捆绑在一起。 (This is mostly found in C projects, not Python.) (这通常在C项目中找到,而不是Python。)

You didn't mention what the "latest" means exactly. 您没有提到“最新”的确切含义。 Is this the latest in pypi? 这是最新的pypi吗?

The best way to make sure a specific version, or greater than a baseline version, of a package is installed is to properly specify the requirement in setup.py requires section. 确保安装特定版本或高于基本版本的软件包的最佳方法是在setup.py require部分中正确指定要求。 Read more about requires here [1]. 在此处[1]了解更多有关require的信息。 This way pip can take care of resolving dependencies, and if it's available in pypi it will be automatic. 这样,pip可以解决依赖关系,如果pypi中可用,它将是自动的。

[1] http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages [1] http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages

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

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