问题
I am attempting to run a python script from excel and am using a vba function to achieve this. I am using xlwings but am getting an error message:
'python' is not recognized as an internal or external command, operable program or batch file.
This occurs when running the following code:
RunPython ("import MarketVolume; MarketVolume.market_volume()")
Thanks in advance, Max
回答1:
You either need to add python to your PATH so that you can start it by typing python on the command prompt or you need to set the full path to the python.exe in your add-in or vba settings for your Interpreter, see http://docs.xlwings.org/en/stable/addin.html#global-settings, see also: http://docs.xlwings.org/en/stable/troubleshooting.html
回答2:
Be sure you have either the xlwings module present in your VBA project(File->Import File-> xlwings.bas under the main xlwings directory) or you have the xlwings add-in installed(xlwings addin install on the command line) and the reference to xlwings set in your VBA project(Tools->References). Then, as Felix said, point your VBA project to your python installation. If you are using the add-in, under 'Interpreter' paste the path to pythonw.exe including the filename. If you are using the module, put the same information next to PYTHONWIN or PYTHONMAC in the config area of the file depending on which OS you're running. As stated in the xlwings documentation, it is preferable to use the add-in over the module, mainly because it makes it a lot easier to manage UDFs.
来源:https://stackoverflow.com/questions/45331563/runpython-usage