如何在SecureCRT的python脚本中引入自己编写的模块

Harmony ·
更新时间:2024-11-13
· 509 次阅读

注:同样可以用来处理importError问题。

1. 重构python解释器(仅限8.0.0版本)

    可以用本地的python 2.7环境来取代SecureCRT本身的解释器

感谢这位博主的两篇博文。

 SecureCRT调用python脚本ImportError https://blog.csdn.net/MrRight17/article/details/82527407  SecureCRT脚本环境类(python)https://blog.csdn.net/MrRight17/article/details/82874479

但是之后的版本之中,改为了vpython.zip 和 vpython.dll就不适用了。 (这里是笔者水平不行。。是在不会)

2. 简单的解决办法,引入模块路径

     参考论坛: https://forums.vandyke.com/printthread.php?t=10331 (使用chrome打开会自动机翻成中文,不如不翻译。。。)

To load a module in a Python script started in SecureCRT you must place the module in a valid path. Valid paths are defined in the sys.path list. If you don't want to use the paths in the list, you can modify sys.path.

Here is an example of how one can modify sys.path to look for the module in the same location as the script:

Code:

import sys
import os
(strScriptPath, strScriptName) = os.path.split(__file__)
# Inject the path to the script into sys.path for use when looking for modules
# to import.
if strScriptPath in sys.path:
    # If the path exists, don't inject.  Unless SecureCRT is closed, we don't
    # need to inject the path of the running script because sys.path is static.
    crt.Dialog.MessageBox("Already There")
else:
    # Inject the path of the running script if it is not in sys.path
    sys.path.insert(0, strScriptPath)

In my tests, I had to use .py rather than .fy for the module name extension.


作者:vince_hamma



python脚本 模块 securecrt Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号