Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 03-07-2012, 07:42 PM
bqbase bqbase is offline
Registered User
 
Join Date: Mar 2012
Posts: 3
Python Import Error: No module named ....

I am trying to import a module into my main script and I always get this error:
----------------------------------------
ImportError

Error: No module named testlib
File: D:\Python\Hello.py
Line: 4

import testlib
----------------------------------------
Below are the two files:
===============================
testlib.fy
----------------------------------------
# $language = "python"
# $interface = "1.0"

def Show(Text):
Text = "this"

===============================
Hello.py
----------------------------------------
# $language = "python"
# $interface = "1.0"

import testlib
----------------------------------------
Please let me know what did I do wrong. I put these in the same directory D:\Python\. Is there a specific directory they need to be in? What if the module is in a different directory? Is there a way to tell the program to look in a specific directory to load the module?

Thanks.
Reply With Quote
  #2  
Old 03-08-2012, 07:33 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi bqbase,

Thanks for the question.

What version of SecureCRT are you using?

What operating system are you using?

Are you able to run this script in an environment where Python is installed, but SecureCRT is not installed?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 03-28-2012, 05:44 PM
bqbase bqbase is offline
Registered User
 
Join Date: Mar 2012
Posts: 3
Secure CRT Version: 6.7.4 (build 354)
Operating system: Windows 7
I am running inside secure CRT. I don't have Python installed.

Thanks.
Reply With Quote
  #4  
Old 03-29-2012, 01:28 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi bqbase,

Thanks for the update.

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.

Does this help you accomplish your goal?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 08-07-2012 at 11:02 AM.
Reply With Quote
  #5  
Old 08-06-2012, 04:58 PM
bqbase bqbase is offline
Registered User
 
Join Date: Mar 2012
Posts: 3
Thanks. I created folder "C:\Program Files\VanDyke Software\SecureCRT\PythonLibraries" and put my python scripts in there and it worked. It would be nice to have this information somewhere in the help file.

However, crt.Dialog.MessageBox() doesn't work in a module. I got this error "global name 'crt' is not defined". I am running SecureCRT 7.0. Is there anyway to get SecureCRT scripting objects to work in a module file. I am running this script inside SecureCRT. I tried using "import SecureCRT" but it still doesn't work.

Below are the two files:
===============================
# $language = "python"
# $interface = "1.0"

import testlib

testlib.testshow("xxx")
----------------------------------------
===============================
testlib.py
----------------------------------------
# $language = "python"
# $interface = "1.0"

def testshow(par):
crt.Dialog.MessageBox("test")
----------------------------------------
Reply With Quote
  #6  
Old 08-07-2012, 11:01 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi bqbase,

Thanks for the update. I have created a request to document what is available using sys.path. Should we document this in the future, we will post to this thread.

If you would like to be notified directly, please complete and submit the form at the following location:
Submit Feature Request
As a note, it may not be documented because it is subject to change. The best method to determine what paths are valid is to use the following code:
Code:
import sys
crt.Dialog.MessageBox(str(sys.path))
With regards to the problem using CRT objects in modules, I have created a feature request to add this capability. Should we add this capability in the future, we will post to this thread also.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 04-30-2013, 11:23 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi bqbase,

Forum user saraza has posted a method for using the crt object in imported modules at the following location:
http://forums.vandyke.com/showthread.php?t=10734
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #8  
Old 10-10-2013, 03:03 PM
quinj quinj is offline
Registered User
 
Join Date: Oct 2013
Posts: 4
Hello,

I tried to use script below to find a valid path to put my scripts that I need to use the import command.

import sys
crt.Dialog.MessageBox(str(sys.path))

One of the Valid spot when I run this is C:\Program Files\VanDyke Software\SecureCRT\PythonLibraries and put e7config.py and test2.py but still getting an

error global name 'crt' is not defined
File C:\Program Files\VanDyke Software\SecureCRT\PythonLibraries\test2.py
Line 7
import e7config



e7config.py

# $language = "python"
# $interface = "1.0"

# This automatically generated script may need to be
# edited in order to work correctly.

def Module():
crt.Screen.Synchronous = True
crt.Screen.Send("set session alarm-notif disabled" + chr(13))
crt.Screen.WaitForString(">")
crt.Screen.Send("set sess page disable" + chr(13))
Module()

test2.py

# $language = "python"
# $interface = "1.0"

# This automatically generated script may need to be
# edited in order to work correctly.

import e7config

e7config.Module()

I am using Windows 7 Professional
SecureCRT Version 7.0.4 (x64 build 537)

Please I need some help on why I am having this problem. TY in advance
Reply With Quote
  #9  
Old 10-10-2013, 04:12 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,937
Hi quinj,

I think you overlooked the most important information in this thread ... a link to a post in another thread where a customer provided an example:

http://forums.vandyke.com/showthread.php?t=10734


So if I understand what you are trying to do and saraza's solution (thanks saraza!), by using pattern-matching, I expect your two scripts would look something like this:


Code:
------ test2.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 e7config
reload(e7config)
e7config.Init(crt)

def Main():
    strOut = e7config.MyModule()
    return

Main()
Code:
------ e7config.py ------
# $language = "python"
# $interface = "1.0"

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

def MyModule():
    crt.Screen.Synchronous = True
    crt.Screen.Send("set session alarm-notif disabled" + chr(13))
    crt.Screen.WaitForString(">")
    crt.Screen.Send("set sess page disable" + chr(13))

I think what you are saying in the early part of your post is that you are saving the scripts to sys.path, so you may not need the varPath statements in test2.py.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #10  
Old 10-11-2013, 02:38 PM
quinj quinj is offline
Registered User
 
Join Date: Oct 2013
Posts: 4
Thank you so much Brenda, the program worked out fine now.
Reply With Quote
  #11  
Old 08-07-2015, 08:15 AM
Caoimhin Caoimhin is offline
Registered User
 
Join Date: Aug 2014
Posts: 35
Angry Similar Issue

I've recently had an issue with a Python script looping through a directory handle and decided it's about time I start using functions. So I bit the bullet and recreated some of my most commonly used code as functions:

Code:
def ReadDirectory (Path):
	import os

	DirectoryList = os.listdir(Path)

	crt.Dialog.MessageBox ("Reading directory: " + Path + 
				         "\n\nDirectory Listing: " + str(DirectoryList) + "\n")

	for Entry in DirectoryList:
		FullPath = Path + "/" + Entry
		if os.path.isdir (FullPath):
			# crt.Dialog.MessageBox (Entry + " is a Directory.")
			ReadDirectory (FullPath)
		if os.path.isfile (FullPath):
			# crt.Dialog.MessageBox (Entry + " is a File.")
			EntrySplit = Entry.split(".")
			Filename = EntrySplit[0]
			FileExtension = EntrySplit[1]
			if Filename != "__FolderData__" and FileExtension == "ini":
				FileList.append (FullPath)
				# crt.Dialog.MessageBox ("Directory: " + Path + 
				# 	                   "\nFile: " + Filename + 
				# 	                   "\n\n ---> File added to Master Session List. <--- " + 
				# 	                   "\n\nSession List: \n" + str(FileList))
		if os.path.islink (FullPath):
			crt.Dialog.MessageBox (Entry + " is a Link.")

	return FileList


import platform
Platform = platform.system()
if Platform == "Linux":
	FirewallPath = "/home/" + User + "/.vandyke/SecureCRT/Config"
if Platform == "Windows":
	FirewallPath = "C:/Users/" + User + "/AppData/Roaming/VanDyke/Config/Sessions/***/Firewalls/US Locations"

FileList = []
ReadDirectory(FirewallPath)
The upper function is located in a module called Utility.py in the directory C:\Scripts\Python. The bottom portion is in another script that is attempting to import Utility. The script calling Utility is getting an error (SecurtCRT Error.png).

Now I've appended to sys.path with "C:\Scripts\Python" with subfolders "Version26" and "Version34" both with an "_init_.py" file in them. Then I tried importing Version26.Utility. No good. I moved Utility.py to C:\Scripts\Python removed the path from sys.path and added the paths to a .pth file stored at C:\Python26 (PTH File.png).

I started up IDLE after cold booting my PC, and found that the paths in the .pth file were added to sys.path, and I could import Utility. Woooot! (IDLE Test.png)

I tried my master script again and it failed to find Utility. Do I need to put a copy of my pth file in the same folder as the SecureCRT scripting engine? Is there a separate PYTHONPATH for SecureCRT?

... --- ...
... --- ...
... --- ...

Last edited by Caoimhin; 08-07-2015 at 08:40 AM.
Reply With Quote
  #12  
Old 08-07-2015, 03:27 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Caoimhin,

If SecureCRT can't load the module, then it doesn't know where it is.

Do you get different results if you place the module in the SecureCRT installation directory?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #13  
Old 08-07-2015, 07:54 PM
Caoimhin Caoimhin is offline
Registered User
 
Join Date: Aug 2014
Posts: 35
Similar Issues

Hey Todd,

No I haven't tried it yet. I kept thinking that SecureCRT's scripting engine will follow the same rules as Python2.6.5 or Python2.6.9 installed on a Windows machine. I walked away from it for a while, but after thinking about it this afternoon I'm curious to see if sys.path run within SecureCRT is going to show the same paths as IDLE.

I'm still a newbie when it comes to Python. I've been reading "Learning Python" by Mark Lutz on O'Reilly. He states that to make a module visible to all others you need to:
  1. Create the module in the same directory as the program.
  2. Add the path where the module is located to PYTHONPATH
  3. Put the module in the standard library directory
  4. Add the path where the module is located to a .pth file and save that pth file in the program directory
  5. Put the module or .pth file in the site-packages home of third-party extensions

Now SecureCRT doesn't have a standard library directory or a site-packages directory. I'll copy the module or .pth into the same directory as the scripting engine, but I'm not sure where it is. Where is the scripting engine located on a Windows machine?
Reply With Quote
  #14  
Old 08-08-2015, 12:07 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Caoimhin,

SecureCRT is not Python. It does include some Python capabilities.

I also don't know what a .pth file is or how (or even if) it might be used with SecureCRT.

My suggestion would be to use the information previously provided in this thread for using modules that you create. If you want to use a module included in Python that SecureCRT doesn't include, we would be able to make a feature request to include it.

The example I provided in post #4 of this thread will inject the script path such that any custom module in the same path as the script should be able to be used.

What version of SecureCRT are you using?

What operating system are you using?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #15  
Old 08-10-2015, 07:28 AM
Caoimhin Caoimhin is offline
Registered User
 
Join Date: Aug 2014
Posts: 35
Similar Issues

I understand that. In fact that's what my previous post was saying. I have Python 2.6 installed to verify commands while I'm scripting, and I do forget that the scripting engine for SecureCRT is a separate environment from Python/IDLE.

Regardless I thought the situation through over the weekend and I solved the problem. So in IDLE I imported sys to get the path (IDLE Test.png).

I then ran sys.path in SecureCRT (SecureCRT Path.png). I took this screenshot after solving the issue. The last entry, "C:\\Scripts\\Python" was not originally there.

The path from within SecureCRT is significantly different with the majority of the strings pointing to C:\\Users\\-Username-\\AppData\\Local\\VanDyke Software\\Clients. It appears that there are 2 entries for paths that don't exist: PythonLibraries and SitePackages. It wouldn't take much to make folders with those names and put any Python modules into those folders.

I would prefer to keep all of my scripts in one place rather than spreading them out into several Python path locations. So I added my .pth file to C:\\Users\\-Username-\\AppData\\Local\\VanDyke Software\\Clients. I then restarted SecureCRT, and viola my path is now known by SecureCRT.

A .pth file is a list of file paths (1 per line) where you are keeping Python modules. This file needs to be stored in the same locations where you would normally store your modules such as, the root directory where your Python executable is stored, the standard library directory, or the site-packages directory.
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 12:33 AM.