如何从批处理文件(parent.bat)执行批处理脚本(child.bat)?原始问题(C#在Venv中以编程方式运行Pip安装) [英] How to execute a batch script (child.bat) from a batch file (parent.bat)? Original Question(C# run Pip install in Venv programmatically)

查看:87
本文介绍了如何从批处理文件(parent.bat)执行批处理脚本(child.bat)?原始问题(C#在Venv中以编程方式运行Pip安装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初的问题是以编程方式在Venv中进行C#运行Pip安装"但事实证明,C#代码或Pip东西没有问题.但是使用批处理脚本.因此,首先是经过编辑的问题,以帮助他人,然后是我的原始问题,仅供记录.

使用命令运行批处理脚本以运行另一个批处理文件.

child.bat

  @echo关闭回声子批处理文件正在运行!在此处执行一些重要的子批处理工作....echo✔子批处理已完成.瞧!回声现在退出Child.bat 

parent.bat

  @echo关闭回声父批处理文件正在运行!在这里做一些重要的事情....回声✔完成.瞧!echo现在运行Child.bat...回声-------------------------------------------------------蝙蝠//注释:以下语句未执行的一些方式回声-------------------------------------------------------回声✔完成.瞧!回声,我相信所有这一切都是完整的.回声我现在要退出. 

输出:

  C:\ Users \ abc>亲子蝙蝠父批处理文件正在运行!!!在这里做一些重要的事情....✔完成了.瞧!!..现在运行Child.bat...-------------------------------------------------------子批处理文件正在运行!!!在这里做一些重要的儿童批处理工作....✔子批处理完成.瞧!现在退出Child.batC:\ Users \ abc> 

其他 parent.bat 回显命令在哪里.

预期输出:

 父批处理文件正在运行!!!在这里做一些重要的事情....✔完成了.瞧!.现在运行Child.bat...-------------------------------------------------------子批处理文件正在运行!!!在这里做一些重要的儿童批处理工作....✔子批处理完成.瞧!现在退出Child.bat-------------------------------------------------------完成了瞧!回声,我相信所有这一切都是完整的.回声,我现在要退出. 



旧问题仅作记录

我有一个C#应用程序,它使用一些python脚本.要运行python脚本,我需要具有一些依赖性.因此,我创建了一个创建VENV的批处理文件.但是它永远不会运行PIP安装命令.

C#代码以运行多个cmd语句.它会创建一个批处理文件.

  public void abc(){var batFileName = Path.Combine(Path.GetTempPath(),$"{Guid.NewGuid()}.bat"));使用(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");//不在venv中运行}var process = new Process();var startInfo =新的ProcessStartInfo{FileName ="cmd.exe",参数= $"/C {batFileName}".};process.StartInfo = startInfo;process.Start();File.Delete(batFileName);} 

批处理文件已创建

  Path_to_python \ python.exe -m venv path_to_venv_folderPath_to_venv_folder \ Scripts \ activate.batpip install rich-//-注释:未在Venv中运行.实际上根本没有运行. 

cmd输出:

  c:\ user \ abc>Path_to_python \ python.exe -m venv path_to_venv_folderc:\ user \ abc>Path_to_venv_folder \ Scripts \ activate.bat(path_to_venv_folder)c:\ user \ abc> 

解决方案

从命令或脚本执行另一个批处理文件时,上下文更改为该批处理文件,并且父文件命令以某种方式丢失.正如 @Squashmas 所说:为了将控制权传递回父批处理文件,必须将子批处理文件用 CALL 命令"

调用

child.bat

  @echo关闭回声子批处理文件正在运行!在此处执行一些重要的子批处理工作....echo✔子批处理已完成.瞧!回声现在退出Child.bat 

parent.bat

  @echo关闭回声父批处理文件正在运行!在这里做一些重要的事情....回声✔完成.瞧!echo现在运行Child.bat...回声-------------------------------------------------------rem注释:CALL将执行.bat并将cmd上下文返回给parent.bat以便进一步执行.呼叫child.bat回声-------------------------------------------------------回声✔完成.瞧!回声,我相信所有这一切都是完整的.回声,我现在要退出. 

记录的旧答案.

因此批处理脚本中的命令应为:

  Path_to_python \ python.exe -m venv path_to_venv_folder呼叫Path_to_venv_folder \ Scripts \ activate.bat点安装丰富 

CALL activate.bat 会将cmd上下文传递回父批处理文件.

我在这里所做的与使用CALL几乎相同.我不建议这样做.使用CALL.

  Path_venv_folder \ Scripts \ activate.bat&&点安装丰富 

由于&& 运算符上下文而在运行第一个命令后,由于命令尚未完成,并且cmd上下文未传递到子批处理脚本,因此父调用cmd仍在在这种情况下, activate.bat 直到执行完整个命令并返回 true 或同等功能为止.

使用 CALL 的输出为:

  c:\ user \ abc>Path_to_python \ python.exe -m venv path_to_venv_folderc:\ user \ abc>呼叫Path_to_venv_folder \ Scripts \ activate.bat(path_to_venv_folder)c:\ user \ abc>点安装丰富安装丰富..............正在安装colorama .......................等等等等等等(path_to_venv_folder)c:\ user \ abc> 

使用&& 的输出为:

  c:\ user \ abc>Path_to_python \ python.exe -m venv path_to_venv_folderc:\ user \ abc>呼叫Path_to_venv_folder \ Scripts \ activate.bat&&点安装丰富安装丰富..............正在安装colorama .......................等等等等等等(path_to_venv_folder)c:\ user \ abc> 

注意,在执行 pip 命令后,将显示 venv 激活的cmd.尽管 pip 是在 venv 的上下文中运行的,但它并不准确,因此我不建议这样做.

PS:我不是批处理脚本专家,所以如果我错了,请纠正我.我会尽快改善它.

一些参考文献:

呼叫|微软文档

My original question was "C# run Pip install in Venv programmatically" but it turns out there was no issue with C# code or the Pip thing. But with the Batch Script. SO first is the edited question to help others and then my original question for just a record.

Run a Batch script with a command to run another Batch file.

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.

Output:

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>

Where are the other parent.bat echo commands.

Expected Output:

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.



Old Question for just a record

I have a C# application and it uses some python scripts. To run the python scripts I need to have some dependencies. So i created a batch file that creates a VENV. But it never runs PIP INSTALL COMMAND.

C# Code to run multiple cmd statements. It creates a batch file.

        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);
        }

batch file created

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.

cmd output:

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> 

解决方案

When another batch file is executed from a command or a script the context is changed to that batch file and the parent file commands are lost somehow. As @Squashmas says "In order for control to be passed back to the parent batch file, the child batch file must be invoked with the CALL command"

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.

Old Answer for the record.

So the commands in the batch script should be:

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 will pass the cmd context back to the parent batch file.

What I did here is almost the same as using CALL. I don't recommend this. Use CALL.

Path_venv_folder\Scripts\activate.bat && pip install rich

After the first command is run due to the && operator context is still with the parent invoked cmd as the command is yet complete and the cmd context is not passed to the child batch script in this case activate.bat until the whole command is executed and true or equivalent is returned.

The output using CALL is:

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>

The output using && is:

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>

Notice the venv activated cmd is shown after the pip command is executed. Although the pip ran in context of the venv it is not accurate and so I don't recommend it.

PS: I am not a batch script expert so please correct me if I am wrong. I will improve it asap.

Some References:

CALL | Microsoft Docs

这篇关于如何从批处理文件(parent.bat)执行批处理脚本(child.bat)?原始问题(C#在Venv中以编程方式运行Pip安装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆