#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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? |
#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. |
#4
|
|||
|
|||
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) Does this help you accomplish your goal? Last edited by rtb; 08-07-2012 at 12:02 PM. |
#5
|
|||
|
|||
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") ---------------------------------------- |
#6
|
|||
|
|||
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 RequestAs 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)) |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
Thank you so much Brenda, the program worked out fine now.
|
#11
|
|||
|
|||
![]()
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) 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. ![]() ... --- ... ... --- ... ... --- ... Last edited by Caoimhin; 08-07-2015 at 09:40 AM. |
#12
|
|||
|
|||
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? |
#13
|
|||
|
|||
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:
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? |
#14
|
|||
|
|||
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? |
#15
|
|||
|
|||
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. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|