【问题标题】:Error: the command ( (from my_script) could not be found within PATH错误:在 PATH 中找不到命令((来自 my_script)
【发布时间】:2020-09-04 09:33:57
【问题描述】:

在使用 Pipenv 和 Pytest 处理 Python FastAPI 项目时,我被要求编写一个 Pipenv 脚本来运行测试。

项目结构如下:

.
├── app
|   ├── main.py
│   ├── my_package
│   │   ├── __init__.py
│   │   ├── (project folders)
│   │   └── tests
|   |       └── tests.py
│   └── __pycache__
└──(deployment scripts and folders, Pipfile, Dokerfile, etc)

我想从顶层结构 (./app/.../tests.py) 在 tests.py 上运行 Pytest。至少现在是这样。

按照here 的建议,我目前从顶部运行它们:

( cd app ; python -m pytest my_package/tests/tests.py )

...按预期工作。

但是,当我添加我的 Pipfile-scripts-section 时:

[scripts]
my_script = "( cd app ; python -m pytest my_package/tests/tests.py )"

...运行它:

pipenv run my_script

我得到错误:

Error: the command ( (from my_script) could not be found within PATH.

我也试过了:

[scripts]
my_script = "cd app && python -m pytest my_package/tests/tests.py"

... 返回另一个类似的错误:

Error: the command cd (from my_script) could not be found within PATH.

所以很明显我把它用作 bash 别名是错误的。 我已尝试搜索有关 [scripts] 部分如何像我一样工作的更多文档,但我没有运气(还)。

【问题讨论】:

  • 如果添加echopath = "echo %PATH%"并运行它,会打印什么?除此之外,pipenv 在我的 Linux 和 MacOS 机器上评估 cd x && ycd 'x && y',找不到目录 x && y,所以这可能不会让你走得太远。
  • 无法按照您的要求执行,因为我收到错误:错误:在 PATH 中找不到命令 echopath(来自 my_script_test)。但感谢您提供有关 pipenv 如何评估 cmd 的信息 :) @wjandrea 的解决方案对我有用。

标签: python bash pipenv


【解决方案1】:

我不熟悉您使用的工具,但错误消息表明它正在寻找可执行文件。 ( 是 shell 语法的一部分,cd 是内置的 shell,而不是可执行文件。试试这个:

my_script = "bash -c 'cd app && python -m pytest my_package/tests/tests.py'"

这里bash 是可执行文件,-c 让它运行你的sn-p。

顺便说一句,请记住cd 可能会失败,因此如果失败,脚本应该退出。

【讨论】:

  • 另外,感谢 '&&',它完全摆脱了困境,也谢谢你
猜你喜欢
  • 2015-06-02
  • 2016-11-20
  • 2018-01-07
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2019-04-19
  • 1970-01-01
相关资源
最近更新 更多