【问题标题】:How to run the script built with pybuilder? Is there `pyb run`?如何运行用pybuilder构建的脚本?有`pyb run`吗?
【发布时间】:2018-04-09 16:24:16
【问题描述】:

请原谅我这个可能微不足道的问题,但是:如何运行 pybuilder 发布的脚本?


我正在努力关注官方Pybuilder Tutorial

我已经完成了这些步骤并成功生成了一个项目

  • 运行单元测试
  • 计算覆盖率
  • 生成setup.py
  • 生成可供pip install使用的.tar.gz

这一切都很好,但我仍然没有看到实际的可运行工件是什么?

target 目录中包含的所有内容似乎与src 目录中的内容以及其他报告和可安装档案中的内容大致相同。

“添加可运行脚本”部分末尾的教程本身得出结论,“脚本已被拾取”。好的,它已被拾取,现在我该如何运行它?本教程绝不会证明我们可以实际打印字符串“Hello, World!”。在屏幕上,尽管事实上整个玩具项目就是这样做的。


MCVE

下面是一个 Bash 脚本,它使用 Python 源文件和构建脚本生成以下目录树:

projectRoot
├── build.py
└── src
    └── main
        ├── python
        │   └── pkgRoot
        │       ├── __init__.py
        │       ├── pkgA
        │       │   ├── __init__.py
        │       │   └── modA.py
        │       └── pkgB
        │           ├── __init__.py
        │           └── modB.py
        └── scripts
            └── entryPointScript.py

7 directories, 7 files
================================================================================
projectRoot/build.py
--------------------------------------------------------------------------------
from pybuilder.core import use_plugin

use_plugin("python.core")
use_plugin("python.distutils")

default_task = "publish"

================================================================================
projectRoot/src/main/scripts/entryPointScript.py
--------------------------------------------------------------------------------
#!/usr/bin/env python

from pkgRoot.pkgB.modB import b

if __name__ == "__main__":
  print(f"Hello, world! 42 * 42 - 42 = {b(42)}")

================================================================================
projectRoot/src/main/python/pkgRoot/pkgA/modA.py
--------------------------------------------------------------------------------
def a(n):
  """Computes square of a number."""
  return n * n

================================================================================
projectRoot/src/main/python/pkgRoot/pkgB/modB.py
--------------------------------------------------------------------------------
from pkgRoot.pkgA.modA import a

def b(n):
  """Evaluates a boring quadratic polynomial."""
  return a(n) - n

生成示例项目的完整脚本(免责声明:按原样提供,修改文件和目录,执行风险自负):

#!/bin/bash

# Creates a very simple hello-world like project
# that can be build with PyBuilder, and describes
# the result.

# Uses BASH heredocs and `cut -d'|' -f2-` to strip
# margin from indented code.

# strict mode
set -eu

# set up directory tree for packages and scripts
ROOTPKG_PATH="projectRoot/src/main/python/pkgRoot"
SCRIPTS_PATH="projectRoot/src/main/scripts"
mkdir -p "$ROOTPKG_PATH/pkgA"
mkdir -p "$ROOTPKG_PATH/pkgB"
mkdir -p "$SCRIPTS_PATH"

# Touch bunch of `__init__.py` files
touch "$ROOTPKG_PATH/__init__.py"
touch "$ROOTPKG_PATH/pkgA/__init__.py"
touch "$ROOTPKG_PATH/pkgB/__init__.py"

# Create module `modA` in package `pkgA`
cut -d'|' -f2- <<__HEREDOC > "$ROOTPKG_PATH/pkgA/modA.py"
  |def a(n):
  |  """Computes square of a number."""
  |  return n * n
  |
__HEREDOC

# Create module `modB` in package `pkgB`
cut -d'|' -f2- <<__HEREDOC > "$ROOTPKG_PATH/pkgB/modB.py"
  |from pkgRoot.pkgA.modA import a
  |
  |def b(n):
  |  """Evaluates a boring quadratic polynomial."""
  |  return a(n) - n
  |
__HEREDOC

# Create a hello-world script in `scripts`:
cut -d'|' -f2- <<__HEREDOC > "$SCRIPTS_PATH/entryPointScript.py"
  |#!/usr/bin/env python
  |
  |from pkgRoot.pkgB.modB import b
  |
  |if __name__ == "__main__":
  |  print(f"Hello, world! 42 * 42 - 42 = {b(42)}")
  |
__HEREDOC

# Create a simple `build.py` build script for PyBuilder
cut -d'|' -f2- <<__HEREDOC > "projectRoot/build.py"
  |from pybuilder.core import use_plugin
  |
  |use_plugin("python.core")
  |use_plugin("python.distutils")
  |
  |default_task = "publish"
  |
__HEREDOC

#################################################
#   Directory tree construction finished, only  #
#   debug output below this box.                #
#################################################

# show the layout of the generater result
tree "projectRoot"

# walk through each python file, show path and content
find "projectRoot" -name "*.py" -print0 | \
while IFS= read -r -d $'\0' pathToFile
do
  if [ -s "$pathToFile" ]
  then
    printf "=%.0s" {1..80} # thick horizontal line
    echo ""
    echo "$pathToFile"
    printf -- "-%.0s" {1..80}
    echo ""
    cat "$pathToFile"
  fi
done

我发现运行新构建的项目的最简单方法如下(从包含projectRoot 的目录中使用):

virtualenv env
source env/bin/activate
cd projectRoot
pyb
cd target/dist/projectRoot-1.0.dev0/dist/
pip install projectRoot-1.0.dev0.tar.gz
entryPointScript.py

这确实成功地运行了脚本及其对用户定义包的所有依赖项,并打印:

Hello, world! 42 * 42 - 42 = 1722

但整个过程似乎相当复杂。作为比较,在 SBT 的类似情况下,我只会发出单曲

run

来自 SBT-shell 的命令——这就是为什么上面的七步配方对我来说有点可疑。

是否有类似pyb runpyb exec 的插件可以做到这一点,但不需要我设置所有这些环境并安装任何东西?我正在寻找的是 SBT 中的 sbt run 或 Maven 中的 mvn exec:java 的类比,它将构建所有内容,设置所有类路径,然后使用 main 方法运行类,而不会在项目目录。

由于源代码和目标输出之间本质上没有区别,我可能缺少一些明显的方式来运行脚本。如果根本不需要PyBuilder 本身,那也没关系:我只想以某种方式在终端中打印Hello, world! 42 * 42 - 42 = 1722-string。

【问题讨论】:

    标签: python virtualenv pybuilder


    【解决方案1】:

    在 build.py 中创建任务

    @task
    def run(project):
        path.append("src/main/python")
        from test_pack import test_app
        test_app.main()
    

    试试:

    pyb run

    【讨论】:

      【解决方案2】:

      显然是以下工作流程:

      • pyb 发布
      • 点安装.tar.gz
      • runMyScript.py
      • 卸载

      正是 PyBuilder 的创建者in this talk 提出的。

      请注意,链接的视频来自 2014 年。如果有人可以提出最近提供的更简化的解决方案,我当然会接受。

      【讨论】:

      • 我只是好奇,你想要一个 exe 或类似的东西吗?您所描述的运行脚本的方式在我看来是一种非常标准的 Python 工作流程
      • @HFBrowning 我需要exe 吗?我实际上不想要exe。我认为 Python 代码可以按原样立即执行,但我不知道如何为一组相互依赖的包、模块和脚本相互连接。在 SBT 中,我只需发出命令runruns,一旦完成,它不会在任何地方留下任何痕迹。在这里,我好像不得不离开项目目录,在别处创建一些完全不相关的环境,然后安装东西,然后然后运行它,然后别忘了清理。这看起来非常复杂。
      • @HFBrowning 也许我忽略了一些显而易见的事情,也许有一些 python --include-all-modules-in-this-package-directory-tree 选项可以帮助我运行复杂的项目。但现在,我只是不明白如何轻松运行包含多个目录和一堆模块的项目。
      • 如此习惯于必须设置 virtualenv 并将软件包安装到其中,也许我没有正确解释您的问题。我通常的工作流程是创建一个 virtualenv > 使用 pip requirements.txt 将包安装到其中 > 创建一个新项目 > 开始工作。这就是 Anaconda 发行版如此受欢迎的原因,因为坦率地说,当您尝试管理一堆不同的(科学计算)软件包时,这种方法很糟糕。
      • 然而,virtualenv 的东西非常接近你想要的。激活它的目的是让它指向正确的解释器和安装到该环境中的包。完成后您也不必卸载任何东西;只是deactivate。再说一次,也许我在谈论与你不同的东西:)
      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      相关资源
      最近更新 更多