开发者

Should I conditionally require simplejson in my setup.py?

开发者 https://www.devze.com 2023-02-10 11:47 出处:网络
I\'m importing simplejson/json conditionally in my module like so: try: import simplejson as json except ImportError:

I'm importing simplejson/json conditionally in my module like so:

try:
    import simplejson as json
except ImportError:
    import json

In my setup.py, however, I don't want to require simplejson if the user has json from the standard libraries. I could do t开发者_Go百科hat like so:

requires = ['kitchen']
try:
    import simplejson
except ImportError:
    requires.append('simplejson')

setup(..., requires=requires)

Is this a good practice for setup.py files? Should I use something else? Should I just outright require simplejson?


Short answer - no, this is not a good practice.

One of key concepts of using distutils, zc.buildout and so on is repeatability. When you add such conditionals you cannot package an egg because it will only work on some machines. When you install it manually on each machine, differences between simplejson and json may break your application. Imagine some bugs you don't know anything about, etc. You would have to make sure it works with both libraries. Best practice is to explicitly require not only the dependency, but also the exact version.

0

精彩评论

暂无评论...
验证码 换一张
取 消