它允许你在不需要先激活虚拟环境的情况下运行安装在其中的black
(或run
后面的任何命令)。
相关提示在poetry run
文档中(我强调):
run
命令在项目的虚拟环境中执行给定的命令。
假设你有一个包含main.py的poetry-demo项目,并且你已经安装了black
:
poetry-demo$ ls
README.rst main.py poetry.lock poetry_demo pyproject.toml tests
poetry-demo$ poetry add black
The following packages are already present in the pyproject.toml and will be skipped:
• black
...
poetry shell
命令),而且 如果你的系统中没有安装 black
,那么简单地执行 black file.py
就会失败。poetry-demo$ which black
poetry-demo$ black main.py
-bash: black: command not found
有了poetry run
,即使不激活虚拟环境,您也可以运行black
:
poetry-demo$ poetry run black main.py
All done! ✨ ✨
1 file left unchanged.
poetry-demo$ poetry shell
Spawning shell within /path/to/virtualenvs/poetry-demo-hCA44HQ0-py3.8
poetry-demo$ . /path/to/virtualenvs/poetry-demo-hCA44HQ0-py3.8/bin/activate
(poetry-demo-hCA44HQ0-py3.8) poetry-demo$ black main.py
All done! ✨ ✨
1 file left unchanged.
(poetry-demo-hCA44HQ0-py3.8) poetry-demo$ poetry run black main.py
All done! ✨ ✨
1 file left unchanged.