在Python脚本中使用easy_install?

16 投票
5 回答
7281 浏览
提问于 2025-04-15 11:58

easy_install 是一个可以让你从控制台安装 Python 扩展的工具,使用起来很简单,比如你可以这样输入:

easy_install py2app

但是,能不能在 Python 脚本里直接使用 easy_install 的功能呢?也就是说,不用像这样调用 os.system("easy_install py2app"),而是把 easy_install 当作一个 Python 模块导入,然后使用它里面的方法?

5 个回答

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文件自动安装这个依赖,除非用户另有说明。你可以在这个链接找到更多相关文档(不过要注意,setuptools的网站速度非常慢!)。

如果你有一些无法通过这种方式满足的需求,可能需要看看S.Lott的回答(不过我自己没有尝试过)。

4

在编程中,有时候我们需要处理一些数据,这些数据可能来自不同的地方,比如用户输入、文件或者网络请求。为了让程序能够理解这些数据,我们通常需要将它们转换成程序能处理的格式。

比如说,如果你从一个网页上获取了一些信息,这些信息可能是以文本的形式存在的。为了让程序能够使用这些信息,我们需要把它们转换成一个合适的数据结构,比如列表或者字典。这样,程序才能更方便地进行操作。

在这个过程中,我们可能会用到一些工具或者库,这些工具可以帮助我们更轻松地完成数据转换的工作。通过这些工具,我们可以快速地把原始数据整理成我们需要的格式,从而提高工作效率。

总之,数据转换是编程中一个非常重要的步骤,它帮助我们把不同来源的数据变得统一和易于使用。

from setuptools.command import easy_install

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

install_with_easyinstall('py2app')
18

当我查看这个安装工具的源代码时,发现你可以尝试以下方法。

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

撰写回答