Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-29-2012, 07:01 AM
CBradley CBradley is offline
Registered User
 
Join Date: Jul 2012
Posts: 5
Python Module importing

I see there are not many posts on this topic, which I take to be a good sign.

My situation is that I am moving from VBScript to Python and am trying to use modules to allow code reuse among several scripts. The problem comes when the module being called uses a crt.x.y call. The error msg states that global name 'crt' is not defined.

My code for the calling module is as follows:

#$language = "Python"
#$interface = "1.0"
import SecureCRT, sys, os
#Define current working directory along w/ Includes file for module location
varPath = os.path.dirname(__file__) + "\Includes"
#Test to see if varPath is already in the system path. If not, add to system path.
if varPath not in sys.path:
sys.path.insert(0, varPath)
#import test module
import test
#reload test module so that changes will be reflected
reload (test)
#call testDef within test module
strOut = test.testDef()
#messagebox the return string
crt.Dialog.MessageBox(StrOut)


The test module code is as follows:

import SecureCRT
def testDef():
crt.Dialog.MessageBox("Hello World!")
return "It was the best of times, it was the worst of times"






I am running this solely on SecureCRT. As a side note, if I run the test module w/ the crt.Dialog.MessageBox line disabled, it runs fine. However, you can see the limitation that would cause.

The idea is to eventually deploy the final product to users who will use SecureCRT. We are currently running SecureCRT 6.7.

Thanks
Clayton
Reply With Quote
  #2  
Old 10-29-2012, 07:34 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Clayton,

Thanks for the post. It is not currently possible to use the CRT object in imported Python modules. We will post to this thread if this capability is added in the future.

If you would like to be notified directly, please complete and submit the form at the following location:
Submit Feature Request
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 04-30-2013, 09:06 AM
saraza saraza is offline
Registered User
 
Join Date: Apr 2013
Posts: 2
A workaround.

A workaround for this problem:

Code:
------ script.py ------
#$language = "Python"
#$interface = "1.0"

import SecureCRT, sys, os
varPath = os.path.dirname(__file__)
if varPath not in sys.path:
    sys.path.insert(0, varPath)

import modulo
modulo.Init(crt)

def Main():
    strOut = modulo.testDef()
    crt.Dialog.MessageBox(strOut)
    return

Main()

Code:
------ modulo.py --------
#$language = "Python"
#$interface = "1.0"

crt = None

def Init(obj):
    global crt
    crt = obj
    return

def testDef():
    crt.Dialog.MessageBox("Hello World!")
    return "It was the best of times, it was the worst of times"
The trick is the use of the 'crt' global variable in the 'modulo.py' module.
Reply With Quote
  #4  
Old 04-30-2013, 11:19 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi saraza,

Thanks for the information. It is much appreciated.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 04-30-2013, 12:35 PM
saraza saraza is offline
Registered User
 
Join Date: Apr 2013
Posts: 2
Edit: added the reload command and the crt variable declaration is not necessary:

Code:
------ script.py ------
#$language = "Python"
#$interface = "1.0"

import SecureCRT, sys, os
varPath = os.path.dirname(__file__)
if varPath not in sys.path:
    sys.path.insert(0, varPath)

import modulo
reload(modulo)
modulo.Init(crt)

def Main():
    strOut = modulo.testDef()
    crt.Dialog.MessageBox(strOut)
    return

Main()
Code:
------ modulo.py --------
#$language = "Python"
#$interface = "1.0"

def Init(obj):
    global crt
    crt = obj
    return

def testDef():
    crt.Dialog.MessageBox("Hello World!")
    return "It was the best of times, it was the worst of times"
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 09:24 AM.