微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 wscript 运行 python 脚本无法激活 conda

如何解决从 wscript 运行 python 脚本无法激活 conda

出于某些原因,我想从 vbs 脚本执行 python 脚本。当我运行脚本时,似乎没有激活 conda。 pythonfile ImportPandas.py 具有以下代码

print('\n-----------------------\n')
try:
    import pandas as pd
    print('Pandas was successfully imported')
except ImportError as error:
    print('Failed to import pandas\n\n%s\n' %error)
print('\n-----------------------\n')
    
input('Press key to stop')

我已经完成了以下测试,但不知道如何解决这些问题。

1.运行python vbs 脚本:

Dim objShell 
Dim PythonExe
Dim PythonScript

Set ObjShell = CreateObject("Wscript.shell")

PythonExe = """C:\Users\XS693E\Anaconda3\envs\py38\python.exe"""
PythonScript = """C:\Users\XS693E\ImportPandas.py"""

objShell.Run PythonExe & " " & PythonScript

Result 1

2.不启动脚本就启动python

如果我选择只启动 python 而不执行脚本,我会收到一个警告,提示 conda 未激活,我认为这是根本问题。

Dim objShell 
Dim PythonExe

Set ObjShell = CreateObject("Wscript.shell")

PythonExe = """C:\Users\XS693E\Anaconda3\envs\py38\python.exe"""

objShell.Run PythonExe

Result 2

3.在 Anaconda Promptshell 中直接导入

Result 3

解决方法

我设法找到了解决方法,这是可视化基本代码。仅当路径中有空格时才需要三引号。

Dim objShell 
Dim PythonExe
Dim PythonScript

Set ObjShell = CreateObject("Wscript.shell")

PythonExe = "conda activate py38  & python "
PythonScript = """C:\Users\XS693E\ImportPandas.py"""

objShell.Run "cmd /C " & PythonExe & PythonScript

此脚本相当于在命令行中直接将以下内容写入终端。

Equivalent command line

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。