如何从 setup.py 中提取依赖信息 [英] How to extract dependencies information from a setup.py

查看:59
本文介绍了如何从 setup.py 中提取依赖信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python项目,姑且称之为foobar,和所有Python项目一样,项目根目录下有一个setup.py脚本.例如

  • foobar
    • setup.py

setup.py 文件内容:

from ez_setup import use_setuptoolsuse_setuptools()从 setuptools 导入设置,find_packages设置(名称='foobar',版本='0.0.0',包=find_packages(),安装要求=['垃圾邮件==1.2.3','鸡蛋> = 4.5.6',],)

我需要使用 Python 从那个 setup.py 文件中获取依赖项信息.我想要的部分是

<预><代码>['垃圾邮件==1.2.3','鸡蛋> = 4.5.6',]

在上面的例子中.我不想安装这个包,我需要的只是依赖信息.当然,我可以使用正则表达式来解析它,但那会很丑,我也可以使用 Python AST 来解析它,但我认为应该已经有一些工具可以做到这一点.这样做的最佳方法是什么?

解决方案

您可以使用 distutils.core 的 run_setup:

from distutils.core import run_setupresult = run_setup("./setup.py", stop_after="init")result.install_requires['垃圾邮件==1.2.3','鸡蛋>=4.5.6']

这样就无需模拟任何内容,并且您可以提取有关项目的更多信息,而不是模拟 setup() 调用.

请注意,此解决方案可能有问题,因为显然正在积极开展工作以弃用 distutils.详情见评论.

I have a python project, let's call it foobar, there is a setup.py script in project root directory like all Python projects. For example

  • foobar
    • setup.py

setup.py file content:

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup, find_packages
setup(
    name='foobar',
    version='0.0.0',
    packages=find_packages(),
    install_requires=[
        'spam==1.2.3',
        'eggs>=4.5.6',
    ],
)

I need to get dependencies information from that setup.py file using Python. The part I want would be

[
    'spam==1.2.3',
    'eggs>=4.5.6',
]

in the example above. I don't want to install this package, all I need is the dependencies information. Certainly, I can use regular expression to parse it, but that would be ugly, I can also use Python AST to parse it, but I think there should already be some tool can do this. What is the best way to do so?

解决方案

You can use distutils.core's run_setup:

from distutils.core import run_setup

result = run_setup("./setup.py", stop_after="init")
result.install_requires
['spam==1.2.3', 'eggs>=4.5.6']

This way there is no need to mock anything and you can potentially extract more information about the project than would be possible by mocking the setup() call.

Note that this solution might be problematic as there apparently is active work being done to deprecate distutils. See comments for details.

这篇关于如何从 setup.py 中提取依赖信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆