开发者

Require BeautifulSoup in a Python Package - What's needed in setup.py?

I'm writing a setup script for a python distribution, foo. My code requires BeautifulSoup, so currently my directory is structured like so:

<root>/
        setup.py
        __init__.py
        foo.py
        BeautifulSoup/
            __init__.py
            BeautifulSoup.py
            etc.

And setup.py currently looks like this (less meta info):

setup(name='foo',
      version='0.9.0',
      py_modules=['foo']
      )

I want to include BeautifulSoup in case the user doesn't have it installed already, but I also don't want to install it if they already have it installed at a particular version. I noticed in the Python 2.7.2 docs that I should included packages=[...] in my setup.py file.

However, Section 2.4. Relationships between Distributions and Packages mentions that there's a way to specify that a particular version of the package is required. I couldn't find any examples of how to use a "requires expression" within setup.py, so I'm not sure if this is what I need.

In short, I need a way to say:

This package requires BeautifulSoup, with at least X.X.X version. If that version isn't installed, use the one that's provided.

How do I do tha开发者_C百科t?


Directory structure:

<root>/
        setup.py
        foo.py

Note: there is no __init__.py file.

You could use distribute to specify dependencies, setup.py:

from setuptools import setup

setup(name='foo',
      version='0.9.0',
      py_modules=['foo'],
      install_requires=['BeautifulSoup >= X.X.X'],
)

This will install required version of BeautifulSoup if it is not already present. You don't need to provide BeautifulSoup in this case.

If you don't want to install BeautifulSoup automatically:

<root>/
        setup.py
        foobar/
            __init__.py
            foo.py
            BeautifulSoup/
                __init__.py
                BeautifulSoup.py
                etc.

setup.py:

from setuptools import setup, find_packages

setup(name='foobar',
      version='0.9.0',
      packages=find_packages(),
) #NOTE: no install_requires

Somewhere in in your modules:

import pkg_resources
try:
    pkg_resources.require("BeautifulSoup>=X.X.X")
except pkg_resources.ResolutionError:
    from foobar import BeautifulSoup
else:
    import BeautifulSoup

It is a less desirable and unusual method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜