本文介绍了一批相当于"源"在Windows上:如何从virtualenv中运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做了bash脚本编程的Windows上的公平一点,但很少批处理脚本。我想在脚本退出时激活蟒蛇的virtualenv,运行一个python脚本,然后停用的virtualenv。

So, I've done a fair bit of bash scripting, but very little batch scripting on Windows. I'm trying to activate a python virtualenv, run a python script, then deactivate the virtualenv when the script exits.

我有一个名为ENV的文件夹,这是我的virtualenv,和一个叫工作文件夹,其中包含我的剧本

I've got a folder called env, which is my virtualenv, and a folder called work, which contains my scripts

这是我到目前为止有:

%~dp0env\Scripts\activate.bat
python %~dp0work\script.py
deactivate

然而,当我运行该脚本,它会激活那么的virtualenv停止。它没有得到到第二线路和运行Python脚本。有什么办法来源的剧本激活文件夹,使批处理脚本的其余部分可以,好像我叫activate.bat在命令行中运行?

However, when I run the script, it activates the virtualenv then stops. It does not get to the second line and run the Python script. Is there any way to "source" the activate script folder, so that the rest of the batch script can be run as if I'd called activate.bat from the command line?

推荐答案

我说,你只需要prePEND'叫'到您activate.bat调用,以确保当前的批处理文件激活后恢复执行:

I'd say you just need to prepend 'call' to your activate.bat invocation, to ensure that the current batch file is resumed after activate is executed:

呼叫%〜dp0env \\脚本\\ activate.bat

call %~dp0env\Scripts\activate.bat

考虑做同样的deactivate.bat。此外,如果要确保当前的cmd.exe环境不被污染电话到您的批处理文件,考虑在SETLOCAL / ENDLOCAL命令对包装你的命令。

Consider doing the same for deactivate.bat . Furthermore, if you want to ensure that the current cmd.exe environment is not polluted by a call to your batch file, consider wrapping your commands in a setlocal/endlocal command pair.

这篇关于一批相当于"源"在Windows上:如何从virtualenv中运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 14:09
查看更多