From: Alan F. <Ala...@sa...> - 2007-02-15 16:32:22
|
Charlie, Well that did it! Thanks a lot for the suggestion. So now how do I = figure out what is causing this? I have looked in the ext directory of = my JVM, and I have these 3 JAR files: dnsns.jar sunjce_provider.jar sunpkcs11.jar None of these has anything in it that looks like "SAS". I use this = method to print the current CLASSPATH: def printCP(): """ Prints the contents of the Classpath """ for url in ClassLoader.getSystemClassLoader().getURLs(): print url And here is the output, after I have imported the module file:/C:/jython2.2b1/jython.jar=20 This is the output from sys.path: >>> sys.path ['C:\\SASJythonPackage', '', 'C:\\jython2.2b1', 'C:\\jython2.2b1\\Lib', = '__classpath__'] How do I read the value of __classpath__? Thanks, Alan -----Original Message----- From: Charlie Groves [mailto:cha...@gm...]=20 Sent: Thursday, February 15, 2007 11:16 AM To: Alan Field Cc: jython users Subject: Re: Problems with import and Jython 2.2 Beta1... Did you try sys.path.insert(0, "C:\\SASJythonPackage")? It's getting to = be more of a longshot, but the offending package could still be in your = JVM's ext or you could have a site file that adds it to your sys.path. Charlie On 2/15/07, Alan Field <Ala...@sa...> wrote: > Charlie, > > I was actually pursuing that as the problem, but in my batch file that = I use to start Jython I clear the CLASSPATH. The jython.jar file is the = only entry on my CLASSPATH in either case. (Whether the package is = inside or outside the Lib directory.) SAS.__file__ is None when the = package is outside the Lib directory and has the path to the __init__.py = file if the package is inside the Lib directory. > > Thanks, > Alan > > -----Original Message----- > From: Charlie Groves [mailto:cha...@gm...] > Sent: Thursday, February 15, 2007 1:41 AM > To: Alan Field > Cc: jython users > Subject: Re: Problems with import and Jython 2.2 Beta1... > > Hmm, could there be an incomplete SAS package somewhere in your = classpath? That could account for all of the symptoms here. The = package in your classpath gets imported before the one you append to the = end of sys.path. When you move the package to the Lib directory, it's = tried before the classpath so it works. Could you try inserting your = package directory at the head of sys.path or printing out SAS.__file__ = to see where it's living? > > Charlie > > On 2/13/07, Alan Field <Ala...@sa...> wrote: > > Charlie, > > > > Well, this has gotten much more interesting. For some reason, if I = move my Python package to Jython's Lib directory, it executes correctly. = (I have some changes to make, but the import behavior is correct.) I = have also been able to verify that the following code will work = correctly to add JAR files dynamically in the 2.2 beta. (The method now = adds JAR files to the sys.path, and the directory to sys.path and = packageManager.addClassPath()) I have not completely tested this = approach, but it seems to work in my preliminary testing. > > > > def add_module_jars(jar_path): > > """ > > This function makes every JAR files in the supplied path > > dynamically loadable without being on the CLASSPATH environment = variable. > > Do not add the entry to the CLASSPATH if it already exists. > > """ > > if exists(jar_path): > > print "Adding jars from directory =3D '" + jar_path + "'" > > > > all_urls =3D=20 > > list(ClassLoader.getSystemClassLoader().getURLs()) > > > > #Add the directory to the Classpath > > try: > > if File(jar_path).toURL() not in all_urls: > > path.append(jar_path) > > packageManager.addClassPath(jar_path) > > except IOException: > > print "Unable to add '" + jar_path + "' to Classpath" > > > > #Add the jars in the directory to the Classpath > > for jar in glob(jar_path + sep + "*.jar"): > > try: > > if File(jar).toURL() not in all_urls: > > path.append(jar) > > except IOException: > > print "Unable to add '" + jar + "' to Classpath" > > > > However, what is really strange is that if my package is not in the = Jython Lib directory, then the imports fail. What is really strange = about this failure is that the import does not execute code in the = __init__.py or module files. I have deleted the class files, and they = are not regenerated. This is the part of the directory structure of my = package: > > > > SAS > > | > > -> __init__.py (contains various utility routines including > > -> add_module_jars) DIServer > > -> __init__.py (contains a class called MDOContext) > > -> support > > -> CreditRisk > > -> MAClient > > > > Normally, I can execute a statement like this in the console to = import MDOContext: > > > > from SAS.DIServer import MDOContext > > > > However, in the 2.2 beta, this produces an error: > > > > Jython 2.2b1 on java1.4.2_09 (JIT: null) Type "copyright", "credits" > > or "license" for more information. > > >>> import sys > > >>> sys.path.append("C:\\SASJythonPackage") > > >>> from SAS.DIServer import MDOContext > > Traceback (innermost last): > > File "<console>", line 1, in ? > > ImportError: no module named DIServer > > > > If I step back and try to import SAS directly, then I can see that = even though the console has not generated an error, the import did not = work correctly. > > > > Jython 2.2b1 on java1.4.2_09 (JIT: null) Type "copyright", "credits" > > or "license" for more information. > > >>> import sys > > >>> sys.path.append("C:\\SASJythonPackage") > > >>> import SAS > > >>> dir() > > ['SAS', '__doc__', '__name__', 'sys'] > > >>> dir(SAS) > > ['CreditRisk', 'DIServer', 'MAClient', '__name__', 'support'] > > >>> > > > > When I execute the dir(SAS) command, it should look something like = this: > > Jython 2.1 on java1.4.2_09 (JIT: null) Type "copyright", "credits"=20 > > or "license" for more information. > > >>> import sys > > >>> sys.path.append("C:\\SASJythonPackage") > > >>> import SAS > > >>> dir(SAS) > > ['Class', 'ClassLoader', 'ConfigParser', 'File', 'Hashtable',=20 > > 'IOException', 'SASJythonPackagePath', 'SAS_TIME_OFFSET',=20 > > 'ST_ATIME', 'System', '__all__', '__doc__', '__file__', '__name__',=20 > > '__path__', '_read_users', '_write_users', 'add_module_jars',=20 > > 'array', 'choice', 'client_stats_path', 'copyfile', 'exists',=20 > > 'get_key_list', 'get_user', 'getcwd', 'glob', 'global_locks_path',=20 > > 'local_stat_path', 'lock', 'log', 'makedirs', 'my_name_is',=20 > > 'normcase', 'olderthan', 'packageManager', 'path', 'permute',=20 > > 'printCP', 'read_config_file', 'remove', 'remove_list_duplicates',=20 > > 'restore_user', 'rmdir', 'sasjythonpackage_path', 'sep',=20 > > 'setup_users', 'sleep', 'stat', 'stub_client_side_job',=20 > > 'stub_job_class', 'stub_tracker', 'system', 'time'] > > > > I have tried stepping through the import code, and it appears that = SAS is loaded as a module of type "java", instead of coming from a .py = file. Here is the sys.modules output from Jython 2.2 beta: > > > > >>> sys.modules > > {'__builtin__': <module '__builtin__' from 'null'>, 'SAS': <java=20 > > package SAS 1>, 'java': <java package java 2>, 'sys': sys module, > > 'os': <jclass org.python.modules.os 3>, 'stat': <module 'stat' from=20 > > 'C:\jython2.2b1\Lib\stat.py'>, 'sitecustomize': <module=20 > > 'sitecustomize' from 'C:\jython2.2b1\Lib\sitecustomize.py'>, 'site': > > <module 'site' from 'C:\jython2.2b1\Lib\site.py'>, 'javapath':=20 > > <module 'javapath' from 'C:\jython2.2b1\Lib\javapath.py'>, 'javaos': = > > <module 'javaos' from 'C:\jython2.2b1\Lib\javaos.py'>, 'java.io':=20 > > <java package java.io 4>, 'UserDict': <module 'UserDict' from=20 > > 'C:\jython2.2b1\Lib\UserDict.py'>, '__main__': <module '__main__'=20 > > from 'null'>, 'java.lang': <java package java.lang 5>, 'exceptions': > > <jclass org.python.core.exceptions 6>} > > > > And here is the output from Jython 2.1: > > > > {'__main__': <module __main__ at 10276870>, 'os': <jclass=20 > > org.python.modules.os at 18707897>, 'java.lang': <java package=20 > > java.lang at 18610053>, 'fnmatch': <module fnmatch at 18304709>, > > 'strop': None, 'sre': <module sre at 20211730>, 'SAS.glob': None, > > 'glob': <module glob at 27035333>, 'javaos': <module javaos at > > 22293724>, 'copy_reg': <module copy_reg at 21840659>, '__builtin__': > > <module __builtin__ at 24027487>, 'types': <jclass=20 > > org.python.modules.types at 2389016>, 'SAS.java': None, 'SAS.os': > > None, 'SAS.random': None, 'SAS.stat': None, 'SAS.sys': None, = 'random': > > <module random at 21094410>, 'ConfigParser': <module ConfigParser at > > 31174955>, 'time': <jclass org.python.modules.time at 5014562>, > > 'shutil': <module shutil at 24565346>, 'SAS': <module SAS at > > 16046701>, 'java.io': <java package java.io at 19949342>, = 'SAS.time': > > None, 'sre_parse': <module sre_parse at 12715534>, 'jarray': <jclass = > > org.python.modules.jarray at 1816810>, 'sitecustomize': <module=20 > > sitecustomize at > > 599654>, 'stat': <module stat at 12066474>, 'SAS.jarray': None, > > 599654>'thread': <jclass org.python.modules.thread at 31811005>, > > 599654>'java': <java package java at 24798854>, 'site': <module site = > > 599654>at 14740287>, 'sys': sys module, 'java.util': <java package=20 > > 599654>java.util at 7647499>, 'SAS.ConfigParser': None, 'math': > > 599654><jclass org.python.modules.math at 18053010>, '_sre': <jclass = > > 599654>org.python.modules._sre at 17769531>, 'UserDict': <module=20 > > 599654>UserDict at 18073623>, 'string': <module string at 28664226>, > > 599654>'SAS.shutil': None, 'sre_compile': <module sre_compile at > > 599654>18670846>, 'os.path': <module javapath at 33426790>, 're': > > 599654><module re at 2220309>, 'javapath': <module javapath at > > 599654>33426790>, 'exceptions': <jclass org.python.core.exceptions=20 > > 599654>33426790>at > > 599654>4214450>, 'sre_constants': <module sre_constants at=20 > > 599654>4214450>32414915>} > > > > I will keep trying to figure out what is going on, and sorry for the = length of this reply. I was just really confused when moving the package = to the Lib directory worked. > > > > Thanks, > > Alan > > > > > > -----Original Message----- > > From: Charlie Groves [mailto:cha...@gm...] > > Sent: Tuesday, February 13, 2007 2:14 AM > > To: Alan Field > > Cc: jython users > > Subject: Re: [Jython-dev] Problems with import and Jython 2.2 = Beta1... > > > > On 2/11/07, Alan Field <Ala...@sa...> wrote: > > > I have also been able to use a JAR file in 2.2 just by adding it = to system.path. I thought that this was going to be the way that Jython = supported dynamically using JAR files. I am going to start deleting = lines from my modules to try and see how far the import is getting, but = I was just looking for some other clues to try and track this down. > > > > I'm going to look at your code more closely later to see how things = have changed between 2.1 and now, but until I do that could you tell me = what kind of errors you're getting after adding a jar to sys.path? > > > > Thanks, > > Charlie > > > |