如何从批处理文件 (parent.bat) 执行批处理脚本 (child.bat)?原始问题(C# 运行 Pip 以编程方式安装在 Venv 中)
How to execute a batch script (child.bat) from a batch file (parent.bat)? Original Question(C# run Pip install in Venv programmatically)
我最初的问题是“C# 运行 Pip 以编程方式安装在 Venv 中”,但事实证明 C# 代码或 Pip 没有问题。但是使用批处理脚本。所以首先是帮助他人的编辑问题,然后是我的原始问题,仅供记录。
运行 一个带有命令的批处理脚本 运行 另一个批处理文件。
child.bat
@echo off
echo Child Batch File is running !!!
echo Doing some Important Child Batch Stuff here . . . .
echo ✔ Child Batch Completed it. Voila !!
echo Now exiting Child.bat
parent.bat
@echo off
echo Parent Batch File is running !!!
echo Doing some Important Stuff here . . . .
echo ✔ Completed it. Voila !!
echo Now running Child.bat . . .
echo -------------------------------------------------------
child.bat
// Comment: Some how below statements are not executing
echo -------------------------------------------------------
echo ✔ Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
输出:
C:\Users\abc> parent.bat
Parent Batch File is running !!!
Doing some Important Stuff here . . . .
✔ Completed it. Voila !!. .
Now running Child.bat . . .
-------------------------------------------------------
Child Batch File is running !!!
Doing some Important Child Batch Stuff here . . . .
✔ Child Batch Completed it. Voila !!
Now exiting Child.bat
C:\Users\abc>
其他 parent.bat
回显命令在哪里。
预期输出:
Parent Batch File is running !!!
Doing some Important Stuff here . . . .
✔ Completed it. Voila !!. .
Now running Child.bat . . .
-------------------------------------------------------
Child Batch File is running !!!
Doing some Important Child Batch Stuff here . . . .
✔ Child Batch Completed it. Voila !!
Now exiting Child.bat
-------------------------------------------------------
Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
只做记录的老问题
我有一个 C# 应用程序,它使用了一些 python 脚本。对于 运行 python 脚本,我需要一些依赖项。所以我创建了一个创建 VENV 的批处理文件。但它从来没有 运行s PIP 安装命令。
C# 代码到 运行 多个 cmd 语句。它创建一个批处理文件。
public void abc() {
var batFileName = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.bat");
using (var batFile = new StreamWriter(batFileName))
{
var pythonPath = Path.Combine(LocalAppData, ProjectGuid, HolocronFolders["HolocronPython"]);
var venv = Path.Combine(LocalAppData, ProjectGuid, HolocronFolders["HolocronVenv"]);
batFile.WriteLine($"{pythonPath}\python.exe -m venv {venv}");
batFile.WriteLine($"{venv}\Scripts\activate.bat");
batFile.WriteLine($"pip install rich"); // Not running in venv
}
var process = new Process();
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/C {batFileName}"
};
process.StartInfo = startInfo;
process.Start();
File.Delete(batFileName);
}
已创建批处理文件
Path_to_python\python.exe -m venv path_to_venv_folder
Path_to_venv_folder\Scripts\activate.bat
pip install rich --//-- Comments: Not running in Venv. Actually not running at all.
命令输出:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> Path_to_venv_folder\Scripts\activate.bat
(path_to_venv_folder) c:\user\abc>
当从命令或脚本执行另一个批处理文件时,上下文将更改为该批处理文件,并且父文件命令会以某种方式丢失。
正如 @Squashmas 所说“为了将控制传递回父批处理文件,必须使用 CALL
命令调用子批处理文件”
child.bat
@echo off
echo Child Batch File is running !!!
echo Doing some Important Child Batch Stuff here . . . .
echo ✔ Child Batch Completed it. Voila !!
echo Now exiting Child.bat
parent.bat
@echo off
echo Parent Batch File is running !!!
echo Doing some Important Stuff here . . . .
echo ✔ Completed it. Voila !!
echo Now running Child.bat . . .
echo -------------------------------------------------------
rem Comments: CALL will execute the .bat and return the cmd context back to the parent.bat for further execution.
CALL child.bat
echo -------------------------------------------------------
echo ✔ Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
记录在案的旧答案。
所以批处理脚本中的命令应该是:
Path_to_python\python.exe -m venv path_to_venv_folder
CALL Path_to_venv_folder\Scripts\activate.bat
pip install rich
CALL activate.bat
会将 cmd 上下文传递回父批处理文件。
我在这里所做的与使用 CALL 几乎相同。我不推荐这个。使用呼叫。
Path_venv_folder\Scripts\activate.bat && pip install rich
在第一个命令 运行 之后,由于 &&
运算符上下文仍然与父调用的 cmd 一起,因为命令尚未完成并且 cmd 上下文未传递给子批处理脚本在这种情况下 activate.bat
直到执行整个命令并返回 true
或等效项。
使用CALL
的输出是:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> CALL Path_to_venv_folder\Scripts\activate.bat
(path_to_venv_folder) c:\user\abc> pip install rich
installing rich..............
installing colorama....................
blah blah blah
(path_to_venv_folder) c:\user\abc>
使用&&
的输出是:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> CALL Path_to_venv_folder\Scripts\activate.bat && pip install rich
installing rich..............
installing colorama....................
blah blah blah
(path_to_venv_folder) c:\user\abc>
注意 venv
激活的 cmd 在执行 pip
命令后显示。虽然 pip
运行 在 venv
的上下文中并不准确,所以我不推荐它。
PS: 我不是批处理脚本专家所以如果我错了请指正。我会尽快改进的
部分参考资料:
我最初的问题是“C# 运行 Pip 以编程方式安装在 Venv 中”,但事实证明 C# 代码或 Pip 没有问题。但是使用批处理脚本。所以首先是帮助他人的编辑问题,然后是我的原始问题,仅供记录。
运行 一个带有命令的批处理脚本 运行 另一个批处理文件。
child.bat
@echo off
echo Child Batch File is running !!!
echo Doing some Important Child Batch Stuff here . . . .
echo ✔ Child Batch Completed it. Voila !!
echo Now exiting Child.bat
parent.bat
@echo off
echo Parent Batch File is running !!!
echo Doing some Important Stuff here . . . .
echo ✔ Completed it. Voila !!
echo Now running Child.bat . . .
echo -------------------------------------------------------
child.bat
// Comment: Some how below statements are not executing
echo -------------------------------------------------------
echo ✔ Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
输出:
C:\Users\abc> parent.bat
Parent Batch File is running !!!
Doing some Important Stuff here . . . .
✔ Completed it. Voila !!. .
Now running Child.bat . . .
-------------------------------------------------------
Child Batch File is running !!!
Doing some Important Child Batch Stuff here . . . .
✔ Child Batch Completed it. Voila !!
Now exiting Child.bat
C:\Users\abc>
其他 parent.bat
回显命令在哪里。
预期输出:
Parent Batch File is running !!!
Doing some Important Stuff here . . . .
✔ Completed it. Voila !!. .
Now running Child.bat . . .
-------------------------------------------------------
Child Batch File is running !!!
Doing some Important Child Batch Stuff here . . . .
✔ Child Batch Completed it. Voila !!
Now exiting Child.bat
-------------------------------------------------------
Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
只做记录的老问题
我有一个 C# 应用程序,它使用了一些 python 脚本。对于 运行 python 脚本,我需要一些依赖项。所以我创建了一个创建 VENV 的批处理文件。但它从来没有 运行s PIP 安装命令。
C# 代码到 运行 多个 cmd 语句。它创建一个批处理文件。
public void abc() {
var batFileName = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.bat");
using (var batFile = new StreamWriter(batFileName))
{
var pythonPath = Path.Combine(LocalAppData, ProjectGuid, HolocronFolders["HolocronPython"]);
var venv = Path.Combine(LocalAppData, ProjectGuid, HolocronFolders["HolocronVenv"]);
batFile.WriteLine($"{pythonPath}\python.exe -m venv {venv}");
batFile.WriteLine($"{venv}\Scripts\activate.bat");
batFile.WriteLine($"pip install rich"); // Not running in venv
}
var process = new Process();
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/C {batFileName}"
};
process.StartInfo = startInfo;
process.Start();
File.Delete(batFileName);
}
已创建批处理文件
Path_to_python\python.exe -m venv path_to_venv_folder
Path_to_venv_folder\Scripts\activate.bat
pip install rich --//-- Comments: Not running in Venv. Actually not running at all.
命令输出:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> Path_to_venv_folder\Scripts\activate.bat
(path_to_venv_folder) c:\user\abc>
当从命令或脚本执行另一个批处理文件时,上下文将更改为该批处理文件,并且父文件命令会以某种方式丢失。
正如 @Squashmas 所说“为了将控制传递回父批处理文件,必须使用 CALL
命令调用子批处理文件”
child.bat
@echo off
echo Child Batch File is running !!!
echo Doing some Important Child Batch Stuff here . . . .
echo ✔ Child Batch Completed it. Voila !!
echo Now exiting Child.bat
parent.bat
@echo off
echo Parent Batch File is running !!!
echo Doing some Important Stuff here . . . .
echo ✔ Completed it. Voila !!
echo Now running Child.bat . . .
echo -------------------------------------------------------
rem Comments: CALL will execute the .bat and return the cmd context back to the parent.bat for further execution.
CALL child.bat
echo -------------------------------------------------------
echo ✔ Completed it. Voila !!
echo I believe all the this are complete.
echo I am now exiting.
记录在案的旧答案。
所以批处理脚本中的命令应该是:
Path_to_python\python.exe -m venv path_to_venv_folder
CALL Path_to_venv_folder\Scripts\activate.bat
pip install rich
CALL activate.bat
会将 cmd 上下文传递回父批处理文件。
我在这里所做的与使用 CALL 几乎相同。我不推荐这个。使用呼叫。
Path_venv_folder\Scripts\activate.bat && pip install rich
在第一个命令 运行 之后,由于 &&
运算符上下文仍然与父调用的 cmd 一起,因为命令尚未完成并且 cmd 上下文未传递给子批处理脚本在这种情况下 activate.bat
直到执行整个命令并返回 true
或等效项。
使用CALL
的输出是:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> CALL Path_to_venv_folder\Scripts\activate.bat
(path_to_venv_folder) c:\user\abc> pip install rich
installing rich..............
installing colorama....................
blah blah blah
(path_to_venv_folder) c:\user\abc>
使用&&
的输出是:
c:\user\abc> Path_to_python\python.exe -m venv path_to_venv_folder
c:\user\abc> CALL Path_to_venv_folder\Scripts\activate.bat && pip install rich
installing rich..............
installing colorama....................
blah blah blah
(path_to_venv_folder) c:\user\abc>
注意 venv
激活的 cmd 在执行 pip
命令后显示。虽然 pip
运行 在 venv
的上下文中并不准确,所以我不推荐它。
PS: 我不是批处理脚本专家所以如果我错了请指正。我会尽快改进的
部分参考资料: