【问题标题】:different result when running command indirectly though script通过脚本间接运行命令时的不同结果
【发布时间】:2020-09-08 16:48:00
【问题描述】:

我在虚拟环境下使用pylint

直接运行 pylint 时,我得到以下输出

$ pylint src/**/*.py
************* Module main
src/main.py:1:0: C0114: Missing module docstring (missing-module-docstring)
src/main.py:3:0: C0116: Missing function or method docstring (missing-function-docstring)

------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 5.00/10, +0.00)

当相同的命令包含在 bash 脚本中时,执行 bash 脚本会产生不同的输出

$ $SHELL --version | head -n 1
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)

$ which $SHELL
/usr/local/bin/bash

$ cat lint.sh
#!/usr/local/bin/bash
pylint *.py src/**/*.py

$ ./lint.sh
************* Module src/**/*.py
src/**/*.py:1:0: F0001: No module named src/**/*.py (fatal)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

如果lint.shsrc 目录位于同一(根)目录中,则lint.sh 中的src/**/*.py 是正确的。

这里有更多关于执行环境的信息

$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.5 (default, Aug  9 2020, 16:57:39)
[Clang 12.0.0 (clang-1200.0.26.2)]
  1. 为什么直接运行命令与间接运行相同命令产生不同的输出(在 bash 脚本中,我的假设是它与 pylint 直接无关)?
  2. 如何解决才能在 bash 脚本中运行命令?

【问题讨论】:

  • 我希望这是因为您的虚拟环境与您当前的环境不同。如果您在顶部设置source ~/.bash_profile,它的行为是否仍然相同?您的路径也将相对于您的位置。同样,您的 pyenv 可能在某处有一个 cd
  • @bobdylan:所以基本上,我需要做的就是将-l 添加到沙班。然而,当移除 shabang 并且没有 bash 子 shell 时,为什么它会这样呢?
  • 因为你没有明确告诉它使用什么解释器,所以它的行为与你的假设不同。

标签: python python-3.x bash pylint


【解决方案1】:

您在脚本中使用了**。需要激活此功能:

shopt -s globstar
pylint *.py src/**/*.py

很可能,它已在您的交互式 shell 中启用,这就是它在那里工作的原因。您可以通过执行查询其设置

shopt globstar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2019-11-26
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多