简体   繁体   English

从 VBA(或命令行)启动 Anaconda 提示

[英]Launching Anaconda Prompt from VBA (Or Command Line)

So far I was being able to launch my python scripts using Excel VBA by doing something like this:到目前为止,我能够通过执行以下操作使用 Excel VBA 启动我的 python 脚本:

Sub ratesmarkup()
Ratespath = "python " & """S:\Market Risk\Middle Office\Corporate Treasury\Politique Investissement\Python\JTD\Rates Markup.py"""
Cmd_Line = Shell("cmd.exe /S /K" & Ratespath & "&" & "exit", vbHide)
End Sub

Which was working perfectly fine until now.到目前为止,它工作得很好。 I started testing using Anaconda for a new script and my new script uses the pyxlsb module that I imported via Anaconda as I couldn't use that module with regular PyCharm.我开始使用 Anaconda 测试一个新脚本,我的新脚本使用我通过 Anaconda 导入的 pyxlsb 模块,因为我无法将该模块与常规 PyCharm 一起使用。 Long story short, I want to know how could I modify this script to launch the Anaconda prompt instead of the regular cmd.exe Command Line.长话短说,我想知道如何修改此脚本以启动 Anaconda 提示符,而不是常规的 cmd.exe 命令行。

When I launch this into the Anaconda Prompt it works:当我将它启动到 Anaconda Prompt 中时,它可以工作:

python "S:\Market Risk\Middle Office\Corporate Treasury\Politique Investissement\Python\PCBONDS.py"

But when I try launching it from regular Command Line it gives me this error in the terminal:但是当我尝试从常规命令行启动它时,它在终端中给了我这个错误:

"ValueError: Unknown engine: pyxlsb"

Yet when I try to install the said pyxlsb module (although I already have it to my knowledge), it says:然而,当我尝试安装上述 pyxlsb 模块时(尽管据我所知,我已经知道了),它说:

Requirement already satisfied: pyxlsb in c:\python\python-3.5.1\lib\site-packages (1.0.6)

The python on Anaconda is 3.7 while the regular one is 3.5. Anaconda 上的 python 是 3.7,而普通的是 3.5。 I am using the pyxlsb engine in this manner:我以这种方式使用 pyxlsb 引擎:

 pandas.read_excel(engine = "pyxlsb") 

Which if I understood requires a certain version of Python, that is provided to me through Anaconda.如果我理解需要某个版本的 Python,这是通过 Anaconda 提供给我的。 This is at work so I can't just install a different version of standalone Python just like that.这是在工作,所以我不能像那样安装不同版本的独立 Python。 So basically is there any possible way of being able to launch the anaconda based python script through VBA by modifying my currently already existing sub?所以基本上有没有任何可能的方法能够通过修改我当前已经存在的子通过 VBA 启动基于 anaconda 的 python 脚本? Can't find nothing that answers this directly.找不到任何可以直接回答这个问题的东西。

Thanks!!谢谢!!

You need to start virtual environment first.second step is run your script inside the virtual environment.create a batch file including all the steps you need to execute and execute batch file through your VB script.您需要首先启动虚拟环境。第二步是在虚拟环境中运行您的脚本。创建一个批处理文件,包括您需要执行的所有步骤,并通过您的 VB 脚本执行批处理文件。 lets say your windows user is TMF and your virtual environment is myenv .假设您的 windows 用户是TMF而您的虚拟环境是myenv

batch file批处理文件

@echo on
set root=C:\Users\TMF\Anaconda3
call %root%\Scripts\activate.bat %root%
call activate myenv
"C:\Users\TMF\Anaconda3\envs\myenv\python.exe" "S:\Market Risk\Middle Office\Corporate Treasury\Politique Investissement\Python\PCBONDS.py"
pause

To those who like conclusions lol.对于那些喜欢结论的人,哈哈。

My.bat file ended up looking like this: My.bat 文件最终看起来像这样:

@ECHO ON
set root=C:\Users\%1%\Anaconda3
call %root%\Scripts\activate.bat %root%
call activate "S:\Market Risk\Middle Office\Corporate Treasury\Politique 
Investissement\Python"
"S:\Market Risk\Middle Office\Corporate Treasury\Politique 
Investissement\Python\python.exe" "S:\Market Risk\Middle Office\Corporate 
Treasury\Politique Investissement\Python\PCBONDS.py"
PAUSE

Where the %1% holds the value of the variable that I will pass through my VBA script, in this case the username of the user currently using the PC.其中 %1% 保存我将通过我的 VBA 脚本传递的变量的值,在这种情况下是当前使用 PC 的用户的用户名。

The VBA script looks like this: VBA 脚本如下所示:

Sub VABONDS()
User = Environ("UserName")

batpath = "pushd " & """S:\Market Risk\Middle Office\Corporate Treasury\Politique 
Investissement\Python\"""
bathfile = "PCBONDS.bat "

Set CMD = VBA.CreateObject("WScript.Shell")
CMD.Run "cmd.exe /S /K" & batpath & "&" & bathfile & User & "&" & "exit", 
vbNormalFocus, True

End Sub

Where the User = Environ("UserName") allows me to get the username of the user dynamically. User = Environ("UserName") 允许我动态获取用户的用户名。

Just thought I would share as it could help others !!只是想我会分享,因为它可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM