在python脚本中使用easy_install?

时间:2009-06-01 14:39:05

标签: python setuptools

easy_install python扩展允许从控制台安装python egg,如:

easy_install py2app

但是可以在python脚本中访问easy_install功能吗?我的意思是,不调用os.system(“easy_install py2app”),而是将easy_install作为python模块导入并使用它的原生方法?

5 个答案:

答案 0 :(得分:18)

当我查看设置工具源时,您似乎可以尝试以下操作。

from setuptools.command import easy_install
easy_install.main( ["-U","py2app"] )

答案 1 :(得分:4)

from setuptools.command import easy_install

def install_with_easyinstall(package):
    easy_install.main(["-U", package]).

install_with_easyinstall('py2app')

答案 2 :(得分:2)

你具体要做什么?除非你有一些奇怪的要求,否则我建议在setup.py中声明包作为依赖:

from setuptools import setup, find_packages
setup(
    name = "HelloWorld",
    version = "0.1",
    packages = find_packages(),
    scripts = ['say_hello.py'],

    # Project uses reStructuredText, so ensure that the docutils get
    # installed or upgraded on the target machine
    install_requires = ['docutils>=0.3'],

    package_data = {
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # And include any *.msg files found in the 'hello' package, too:
        'hello': ['*.msg'],
    }

    # metadata for upload to PyPI
    author = "Me",
    author_email = "me@example.com",
    description = "This is an Example Package",
    license = "PSF",
    keywords = "hello world example examples",
    url = "http://example.com/HelloWorld/",   # project home page, if any

    # could also include long_description, download_url, classifiers, etc.
)

这里的关键是install_requires = ['docutils>=0.3']。这将导致setup.py文件自动安装此依赖项,除非用户另行指定。您可以在here找到更多文档(请注意,setuptools网站非常慢!)。

如果你确实有某种要求不能满足这种要求,你应该看看S.Lott's answer(虽然我自己从未尝试过)。

答案 3 :(得分:0)

关于调用setuptools.main()的答案是正确的。但是,如果setuptools创建.egg,则安装后脚本将无法导入该模块。在python开始时间,鸡蛋会自动添加到sys.path中。

一种解决方案是使用require()将新蛋添加到路径中:

from setuptools.command import easy_install
import pkg_resources
easy_install.main( ['mymodule'] )
pkg_resources.require('mymodule')

答案 4 :(得分:-2)

我认为你可以通过使用导入setuptools来实现这一点。