Python PEX 加载

6
我一直在尝试理解Python PEX实用程序(https://pex.readthedocs.org/en/latest/),用于将一些应用程序捆绑到.pex文件中进行部署。
我的应用程序不够大,不需要Twitter的Pants构建工具,而且我有一些构建要求Pants无法解决。但是,我尝试使用python_binary来使用Pants工具构建系统,结果是在pex文件的根目录中加载了源文件。Pants中的BUILD文件接受sources属性,用于python_binary,它可以是在构建正在运行的目录中的文件的全局匹配,但是Pants使用的是pex编程API而不是命令行实用程序。
问题在于当我单独使用pex命令行实用程序时,它似乎想要分发(即使用setup.py等设置文件夹)并希望将我的代码安装到.pex文件的.deps文件夹中,而不仅仅是像Pants那样将Python文件复制到pex文件的根目录中。
通过命令行pex工具复制文件(而不是安装包)是否不可能?
1个回答

7

从pex 1.0.0版本开始,没有直接捆绑文件和目录的功能,您必须像您建议的那样拥有一个setup.py,或者使用pants(不再是Twitter的产品-独立的)。

因此,您有三种前进的路径(#1已经知道,但为其他人提供详细信息):

  1. Create a setup.py and point the pex tool at its dir

    $ tree -h
    .
    ├── [4.0K]  lib
    │   ├── [   0]  __init__.py
    │   ├── [  38]  lib.py
    │   └── [  68]  main.py
    └── [  76]  setup.py
    
    1 directory, 4 files
    $ cat lib/lib.py 
    def func():
        return 'func in lib'
    
    $ cat lib/main.py 
    from .lib import func
    
    if __name__ == '__main__':
        print(func())
    
    $ cat setup.py 
    from setuptools import setup
    
    setup(
        name='lib',
        packages=['lib']
    )
    
    $ pex . -e lib.main -o lib.pex
    $ ./lib.pex 
    func in lib
    

    NB: The . in the pex command line is the bit pointing pex at this dir's setup.py

  2. File an issue against pex to support a set of files in place of a requirement / setup.py. You can do that here.

  3. File issues against pants to support the requirements you have that it does not address. You can do that here

作为Pants的提交者,我可以说我们正在努力使Pants变得越来越易于使用,使得没有项目太小。你应该能够pip install pantsbuild.pants.backend.python && touch pants.ini,并在Python-only repo中运行,但今天我们还没有达到那个目标。


将其标记为答案,感谢您清晰地阐述。我会看一下pex,并创建一个问题,看看他们是否会合并一个新选项来支持通配符,如果我能够解决它。我不使用Pants的原因是(我并不是说它是一个糟糕的工具,它实际上非常棒),因为我在一个单体库中构建了各种组件。该库有Python项目,Pants非常适用于这些项目,但它还有大约30个基于JS React的应用程序,需要进行构建以及配置合并、SQL安装/更新等操作,我无法轻松地弄清楚如何在Pants中支持这些操作。 - Jeff U.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接