在setup.py或pip需求文件中,如何控制安装包依赖项的顺序?

发布于 2024-10-17 12:17:13 字数 535 浏览 3 评论 0原文

我有一个 Python 包,其 setup.py 具有通过通常方式声明的依赖项,在 install_requires=[...] 中。其中一个软件包 scikits.timeseries 具有一个 setup.py ,期望已安装 numpy,因此,我想要某种方法来首先安装 numpy。对于这种情况以及一般情况,可以控制依赖项安装的顺序吗?如何?

目前,setup.py 获取依赖项的顺序(如 arg install_requires 中列出的)似乎几乎是随机的。另外,在 setup.py setup(...) 中,我尝试使用 arg:

extras_require={'scikits.timeseries': ['numpy']}

...没有成功,安装依赖项的顺序不受影响。

我还尝试设置 pip 需求文件,但 pip 安装依赖项的顺序与需求文件的行顺序不匹配,所以运气不好。

另一种可能性是在 setup.py 顶部附近进行系统调用,在 setup(...) 调用之前安装 numpy,但我希望有更好的方法。预先感谢您的任何帮助。

I've got a Python package with its setup.py having dependencies declared via the usual way, in install_requires=[...]. One of the packages there, scikits.timeseries, has a setup.py expecting numpy to already be installed, thus, I'd like some way to have numpy installed first. For this case and in general, can the order of dependency installation be controlled? How?

Currently the order in which setup.py pulls down dependencies (as listed in the arg install_requires) seems practically random. Also, in the setup.py setup(...) I tried using the arg:

extras_require={'scikits.timeseries': ['numpy']}

...without success, the order of installing dependencies was unaffected.

I also tried setting up a pip requirements file, but there too, pip's order of installing dependencies didn't match the line-order of the requirements file, so no luck.

Another possibility would be to have a system call near the top of setup.py, to install numpy before the setup(...) call, but I hope there's a better way. Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-10-24 12:17:13

如果 scikits.timeseries 需要 numpy,那么它应该将其声明为依赖项。如果确实如此,那么 pip 将为您处理事情(我很确定 setuptools 也会,但我已经很长一段时间没有使用它了)。如果您控制 scikits.timeseries,那么您应该修复它的依赖声明。

If scikits.timeseries needs numpy, then it should declare it as a dependency. If it did, then pip would handle things for you (I'm pretty sure setuptools would, too, but I haven't used it in a long while). If you control scikits.timeseries, then you should fix it's dependency declarations.

二货你真萌 2024-10-24 12:17:13

使用setup_requires参数,例如安装numpy之前将scipy放入setup_requires中并添加__builtins__.__NUMPY_SETUP__ = False正确安装 numpy 的钩子:

setup(
    name='test',
    version='0.1',
    setup_requires=['numpy'],
    install_requires=['scipy']
)

def run(self):
    __builtins__.__NUMPY_SETUP__ = False
    import numpy

Use setup_requires parameter, for instance to install numpy prior scipy put it into setup_requires and add __builtins__.__NUMPY_SETUP__ = False hook to get numpy installed correctly:

setup(
    name='test',
    version='0.1',
    setup_requires=['numpy'],
    install_requires=['scipy']
)

def run(self):
    __builtins__.__NUMPY_SETUP__ = False
    import numpy
很糊涂小朋友 2024-10-24 12:17:13

这是一个实际有效的解决方案。这不是一个必须诉诸的过于“愉快”的方法,而是“绝望的时候......”。

基本上,您必须:

  • 覆盖 setuptools“安装命令”类(加上密切相关的类似物)
  • 通过命令行语句从脚本执行 pip,您可以强制执行命令

这样做的缺点是:

  • 必须安装 Pip。您不能在没有它的环境中执行setup.py。
  • 由于某些奇怪的原因,初始“先决条件”安装的控制台输出没有出现。 (也许我会在这里发布更新来解决这个问题......)

代码:

from setuptools import setup

# Override standard setuptools commands. 
# Enforce the order of dependency installation.
#-------------------------------------------------
PREREQS = [ "ORDERED-INSTALL-PACKAGE" ]

from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info

def requires( packages ): 
    from os import system
    from sys import executable as PYTHON_PATH
    from pkg_resources import require
    require( "pip" )
    CMD_TMPLT = '"' + PYTHON_PATH + '" -m pip install %s'
    for pkg in packages: system( CMD_TMPLT % (pkg,) )       

class OrderedInstall( install ):
    def run( self ):
        requires( PREREQS )
        install.run( self )        

class OrderedDevelop( develop ):
    def run( self ):
        requires( PREREQS )
        develop.run( self )        

class OrderedEggInfo( egg_info ):
    def run( self ):
        requires( PREREQS )
        egg_info.run( self )        

CMD_CLASSES = { 
     "install" : OrderedInstall
   , "develop" : OrderedDevelop
   , "egg_info": OrderedEggInfo 
}        
#-------------------------------------------------

setup ( 
     ...
    install_requires = [ "UNORDERED-INSTALL-PACKAGE" ],
    cmdclass = CMD_CLASSES
)

Here's a solution which actually works. It's not an overly "pleasant" method to have to resort to, but "desperate times...".

Basically, you have to:

  • Override the setuptools "install command" class (plus the closely related analogs)
  • Execute pip from the script via command line statements for which you can enforce the order

The drawbacks to this are:

  • Pip must be installed. You can't just execute setup.py in an environment without that.
  • The console output of the initial "prerequisite" installs don't appear for some weird reason. (Perhaps I'll post an update here down the line fixing that...)

The code:

from setuptools import setup

# Override standard setuptools commands. 
# Enforce the order of dependency installation.
#-------------------------------------------------
PREREQS = [ "ORDERED-INSTALL-PACKAGE" ]

from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info

def requires( packages ): 
    from os import system
    from sys import executable as PYTHON_PATH
    from pkg_resources import require
    require( "pip" )
    CMD_TMPLT = '"' + PYTHON_PATH + '" -m pip install %s'
    for pkg in packages: system( CMD_TMPLT % (pkg,) )       

class OrderedInstall( install ):
    def run( self ):
        requires( PREREQS )
        install.run( self )        

class OrderedDevelop( develop ):
    def run( self ):
        requires( PREREQS )
        develop.run( self )        

class OrderedEggInfo( egg_info ):
    def run( self ):
        requires( PREREQS )
        egg_info.run( self )        

CMD_CLASSES = { 
     "install" : OrderedInstall
   , "develop" : OrderedDevelop
   , "egg_info": OrderedEggInfo 
}        
#-------------------------------------------------

setup ( 
     ...
    install_requires = [ "UNORDERED-INSTALL-PACKAGE" ],
    cmdclass = CMD_CLASSES
)
笑咖 2024-10-24 12:17:13

您可以将 numpy 添加到 setup_requires 部分:

setup_requires=['numpy'],

You can add numpy to setup_requires section:

setup_requires=['numpy'],
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文