批处理:启动pipenv shell,然后在虚拟环境中运行命令。

8

这里是一个批处理脚本:

Z:
cd Z:\different_directory
pipenv shell
cd ..\another_directory


:End
cmd  /k

这里发生的情况是pipenv shell被启动,但虚拟环境没有进行cd操作。相反,在我退出pipenv后,它会运行cd命令。

使用此批处理脚本是否可以从pipenv内部运行命令?

2个回答

15

您可以使用pipenv run代替pipenv shell直接运行Python命令或批处理脚本。您不能直接运行pipenv run cd ../another_dir,但我假设这不是主要目标,因为您只会在那个会话中更改目录。您可以创建一个批处理脚本,比如说test.bat, 包含以下内容:

cd ../another_dir
python test.py
然后使用pipenv run test.bat运行它。下面的页面有更多详细信息。

来源: http://witkowskibartosz.com/blog/pipenv_run_vs_pipenv_shell.html#.W2SBZflKhaQ


你是不是想说 pipenv runpipenv shell 而不是 python runpython shell - A__

7
我知道这个问题很久了,但如果你们中有人是通过谷歌找到这篇文章的,这里是确切的答案:只需使用pipenv shell "/c command [args]",在这个例子中是pipenv shell "/c cd ..\another_directory"
为什么它有效:就像文档所述,shell之后的任何内容都会被作为子shell的参数传递,并且CMD上的/c参数可以在shell spawn上运行命令。所以这里就是这样。你甚至可以让它运行整个.bat文件。

2
在Ubuntu环境下,pipenv shell "cd ..\another_directory" 对我很有效。 - Eron Salling

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