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 03-30-2020, 10:21 AM
pjano1 pjano1 is offline
Registered User
 
Join Date: Mar 2020
Posts: 7
How to import a class file into another file that uses SecureCRT

Hello,

I'm trying to import a class I created into different, separate scripts. Both the script and the class use python through SecureCRT. Whenever I copy and paste the entire class into the calling script and call it this way:


class A:
...

def main():
a = A()


It works fine. But if I try and import it like this:


import a_file

def main():
a = a_file.A()


or like this:


from a_file import A

def main():
a = A()


It says "global name 'crt' is not defined" and won't work. I would much rather be able to define my class in a different file so I don't have to keep copying and pasting the entire thing every time I need to use it.

I've run into a similar issue before, only with function scripts I've written (they only hold functions). I found a work around for this I on another forum here a while ago, but essentially just put the following syntax in the calling script before it gets going:


import function_a
reload(function_a)
function_a.init(crt)


And then add the following function definition in the function script you need to call:


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


I've been playing around with this to see if I can get it to work on classes as well as function scripts, but so far I've come up empty. What baffles me is why what I just put works for SecureCRT python function scripts, but the moment you try this on a script with a class definition, it doesn't work. Has anyone run into this issue before and or were able to fix it/work around it?

Thanks
Reply With Quote
  #2  
Old 03-30-2020, 11:02 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,439
Hi pjano1,

Check out the info in this forum post.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 03-31-2020, 06:26 AM
pjano1 pjano1 is offline
Registered User
 
Join Date: Mar 2020
Posts: 7
Hi Brenda,

I'm familiar with the forum post you referred me to.

This doesn't consistently work with a file that only contains classes. I got my code to work temporarily using the forum post, but then SecureCRT crashed and I was back to square one with the same error.
Reply With Quote
  #4  
Old 03-31-2020, 08:09 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,439
Hi pjano1,

What version of SecureCRT are you using?

If v8.5 or v8.7, you should report the crash by sending an email to support@vandyke.com.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

Tags
class , function , import error


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 08:52 PM.