在python脚本中使用easy_install?

在python脚本中使用easy_install?,python,setuptools,Python,Setuptools,easy_install python extension允许从控制台安装python蛋,如: easy_install py2app 但是,有可能在python脚本中访问easy_install功能吗?我的意思是,不调用os.system(“easy\u install py2app”),而是将easy\u install作为python模块导入,并使用其本机方法?我认为您可以通过使用任何一种导入setuptools来实现这一点。当我查看setuptools源代码时,您可以尝试以下方法 fr

easy_install python extension允许从控制台安装python蛋,如:

easy_install py2app

但是,有可能在python脚本中访问easy_install功能吗?我的意思是,不调用os.system(“easy\u install py2app”),而是将easy\u install作为python模块导入,并使用其本机方法?

我认为您可以通过使用任何一种导入setuptools来实现这一点。

当我查看setuptools源代码时,您可以尝试以下方法

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

你具体想做什么?除非您有一些奇怪的要求,否则我建议在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\u requires=['docutils>=0.3']
。这将导致setup.py文件自动安装此依赖项,除非用户另有指定。您可以找到更多关于这方面的文档(请注意,setuptools网站的速度非常慢!)


如果您确实有某种需求无法通过这种方式满足,您可能应该看看(尽管我自己从未尝试过)。

关于调用
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')

以及调用什么方法或在何处查找文档?:)在谷歌搜索中找到:easy_install.main(“-U py2app.split()”)。请更改您的答案文本,以便我可以接受:)我可以确认这是有效的-我一直这样做是为了为我的python项目构建自定义安装脚本+1.
from setuptools.command import easy_install
import pkg_resources
easy_install.main( ['mymodule'] )
pkg_resources.require('mymodule')