How should I handle importing third-party libraries within my setup.py script?

前端 未结 3 646
心在旅途
心在旅途 2021-01-12 08:40

I\'m developing a Python application and in the process of branching off a release. I\'ve got a PyPI server set up on a company server and I\'ve copied a source distribution

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 09:26

    I'm ignoring licensing issues in this answer. You definetly need to take these into account before you really do a release.

    Is it acceptable to rely on third-party libraries like this in setup.py

    Yes, it is acceptable but generally these should be minimized, especially if these are modules which have no obvious use for the end-user. Noone likes to have packages they don't need or use.

    what is the recommended approach to using them?

    There are basically 3 options:

    • Bootstrap them (for example use pip to programmatically install packages). For example setuptools provides an ez_setup.py file that can be used to bootstrap setuptools. Maybe that can be customized to download and install appdirs.

    • Include them (especially if it's a small package) in your project. For example appdirs is basically just a single file module. Pretty easy to copy and maintain in your project. Be very careful with licensing issues when you do that!

    • Fail gracefully when it's not possible to import them and let the user install them. For example:

      try:
          import appdirs
      except ImportError:
          raise ImportError('this package requires "appdirs" to be installed. '
                            'Install it first: "pip install appdirs".')
      

提交回复
热议问题