From: Marcel N. <mar...@gm...> - 2009-06-24 20:14:28
|
Hi, I am porting a large mixed Java / Jython desktop application from Jython 2.2.1 to 2.5. The applications's menu is Jython code. In version 2.2.1, I could simply import a package: import pyinventory and then instantiate at will any class in the package (even with an empty __init__.py for the package): pyinventory.inventory_description.InventoryDescription() where "pyinventory" is a package, "inventory_description" is a module and "InventoryDescription" is a class. it seems as if Jython 2.2.1 was loading the required class only when they were instantiated. Importing only the package does not seem to work in Jython 2.5 (the referenced module is never found). So I changed the imports to import all the needed modules containing classes that will be instantiated in the current module: import pyinventory.inventory_description ... It nows seems as if Jython will load at once all the application in memory when the application is started. Once loaded, the application is a lot faster with Jython 2.5 than it was with version 2.2.1. The only problem is that the applications's startup time went from 4 ~ 5 seconds to 45 ~ 50 seconds. The (Jython) menu is loaded from a Java class containing: static public void main(String args[]) { ... (some initialization code for the application) try { PySystemState.initialize(); PySystemState sys = new PySystemState(); PyStringMap dic = new PyStringMap(); sys.path.append(new PyString("rmn.jar")); sys.path.append(new PyString("lib/jython.jar")); sys.path.append(new PyString("lib/rmnjlib.jar")); sys.path.append(new PyString("lib/jy25lib.jar/Lib")); sys.path.append(new PyString("lib/forms-1.2.1.jar")); sys.path.append(new PyString("lib/swingx-0.9.5.jar")); sys.path.append(new PyString("lib/reportlab-2.3.jar")); sys.path.append(new PyString("lib/jfreechart-1.0.13.jar")); sys.path.append(new PyString("lib/iText-2.1.5.jar")); sys.path.append(new PyString("macros")); interp = new PythonInterpreter(dic, sys); interp.exec("import main"); } catch (PyException ex4) { System.err.println(ex4); } } The tests were made on Windows XP, using Java 6.0. 1) Am I doing something wrong? 2) Is there anything (a configuration switch?) that will force Jython to lazily import the modules when instantiated? Thanks. Marcel |
From: Jim B. <jb...@zy...> - 2009-06-26 05:21:58
|
Marcel, To be compliant with the Python language we no longer support imports in this fashion for Python modules. Python has a philosophy of requiring explicit import of names into the namespace, and we have followed this with Jython 2.5. (However, for Java packages, we still allow this - at least this wouldn't surprise Python code.) But nothing prevents you from writing an import function that does what you want here instead. This includes a couple of options: - Hierarchically traversing a package, importing every module into the namespace - Lazy module imports. http://peak.telecommunity.com/DevCenter/Importingor http://code.activestate.com/recipes/473888/ should work, although neither have unit tests to confirm this impression. (Please report back here and/or against bugs.jython.org if that's not the case.) - Jim On Wed, Jun 24, 2009 at 2:13 PM, Marcel Nepveu <mar...@gm...>wrote: > Hi, > > I am porting a large mixed Java / Jython desktop application from > Jython 2.2.1 to 2.5. The applications's menu is Jython code. > > In version 2.2.1, I could simply import a package: > > import pyinventory > > and then instantiate at will any class in the package (even with an > empty __init__.py for the package): > > pyinventory.inventory_description.InventoryDescription() > > where "pyinventory" is a package, "inventory_description" is a module > and "InventoryDescription" is a class. it seems as if Jython 2.2.1 > was loading the required class only when they were instantiated. > > Importing only the package does not seem to work in Jython 2.5 (the > referenced module is never found). So I changed the imports to import > all the needed modules containing classes that will be instantiated in > the current module: > > import pyinventory.inventory_description > ... > > It nows seems as if Jython will load at once all the application in > memory when the application is started. Once loaded, the application > is a lot faster with Jython 2.5 than it was with version 2.2.1. The > only problem is that the applications's startup time went from 4 ~ 5 > seconds to 45 ~ 50 seconds. > > The (Jython) menu is loaded from a Java class containing: > > static public void main(String args[]) > { > ... (some initialization code for the application) > > try > { PySystemState.initialize(); > > PySystemState sys = new PySystemState(); > PyStringMap dic = new PyStringMap(); > sys.path.append(new PyString("rmn.jar")); > sys.path.append(new PyString("lib/jython.jar")); > sys.path.append(new PyString("lib/rmnjlib.jar")); > sys.path.append(new PyString("lib/jy25lib.jar/Lib")); > sys.path.append(new PyString("lib/forms-1.2.1.jar")); > sys.path.append(new PyString("lib/swingx-0.9.5.jar")); > sys.path.append(new PyString("lib/reportlab-2.3.jar")); > sys.path.append(new PyString("lib/jfreechart-1.0.13.jar")); > sys.path.append(new PyString("lib/iText-2.1.5.jar")); > sys.path.append(new PyString("macros")); > > interp = new PythonInterpreter(dic, sys); > interp.exec("import main"); > } > catch (PyException ex4) > { System.err.println(ex4); > } > } > > The tests were made on Windows XP, using Java 6.0. > > 1) Am I doing something wrong? > > 2) Is there anything (a configuration switch?) that will force Jython > to lazily import the modules when instantiated? > > Thanks. > > Marcel > > > ------------------------------------------------------------------------------ > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > -- Jim Baker jb...@zy... |
From: Marcel N. <mar...@gm...> - 2009-06-27 01:08:15
|
Jim, thanks. This is what I was looking for. I tried both of the lazy module imports. 1) http://code.activestate.com/recipes/473888/ has some issues with Jython 2.5: - with __builtins__ (easily solved) - with at least one standard library module: it will not find _sre, imported from sre_compile.py. 2) http://peak.telecommunity.com/DevCenter/Importing works with Jython 2.5. It needs a "pkg_resources" module (from setuptools). I found one here: http://svn.python.org/projects/sandbox/trunk/setuptools/pkg_resources.py So far, by using "lazyImport" in the Importing package, the applications's startup time was reduced from 45 seconds to 12 seconds. Still testing and trying to shave a few seconds but so far it looks good. Thanks again. Marcel 2009/6/26 Jim Baker <jb...@zy...> > Marcel, > To be compliant with the Python language we no longer support imports in > this fashion for Python modules. Python has a philosophy of requiring > explicit import of names into the namespace, and we have followed this with > Jython 2.5. (However, for Java packages, we still allow this - at least this > wouldn't surprise Python code.) > > But nothing prevents you from writing an import function that does what you > want here instead. This includes a couple of options: > > - Hierarchically traversing a package, importing every module into the > namespace > - Lazy module imports. > http://peak.telecommunity.com/DevCenter/Importing or > http://code.activestate.com/recipes/473888/ should work, although > neither have unit tests to confirm this impression. (Please report back here > and/or against bugs.jython.org if that's not the case.) > > > - Jim > > On Wed, Jun 24, 2009 at 2:13 PM, Marcel Nepveu <mar...@gm...>wrote: > >> Hi, >> >> I am porting a large mixed Java / Jython desktop application from >> Jython 2.2.1 to 2.5. The applications's menu is Jython code. >> >> In version 2.2.1, I could simply import a package: >> >> import pyinventory >> >> and then instantiate at will any class in the package (even with an >> empty __init__.py for the package): >> >> pyinventory.inventory_description.InventoryDescription() >> >> where "pyinventory" is a package, "inventory_description" is a module >> and "InventoryDescription" is a class. it seems as if Jython 2.2.1 >> was loading the required class only when they were instantiated. >> >> Importing only the package does not seem to work in Jython 2.5 (the >> referenced module is never found). So I changed the imports to import >> all the needed modules containing classes that will be instantiated in >> the current module: >> >> import pyinventory.inventory_description >> ... >> >> It nows seems as if Jython will load at once all the application in >> memory when the application is started. Once loaded, the application >> is a lot faster with Jython 2.5 than it was with version 2.2.1. The >> only problem is that the applications's startup time went from 4 ~ 5 >> seconds to 45 ~ 50 seconds. >> >> The (Jython) menu is loaded from a Java class containing: >> >> static public void main(String args[]) >> { >> ... (some initialization code for the application) >> >> try >> { PySystemState.initialize(); >> >> PySystemState sys = new PySystemState(); >> PyStringMap dic = new PyStringMap(); >> sys.path.append(new PyString("rmn.jar")); >> sys.path.append(new PyString("lib/jython.jar")); >> sys.path.append(new PyString("lib/rmnjlib.jar")); >> sys.path.append(new PyString("lib/jy25lib.jar/Lib")); >> sys.path.append(new PyString("lib/forms-1.2.1.jar")); >> sys.path.append(new PyString("lib/swingx-0.9.5.jar")); >> sys.path.append(new PyString("lib/reportlab-2.3.jar")); >> sys.path.append(new PyString("lib/jfreechart-1.0.13.jar")); >> sys.path.append(new PyString("lib/iText-2.1.5.jar")); >> sys.path.append(new PyString("macros")); >> >> interp = new PythonInterpreter(dic, sys); >> interp.exec("import main"); >> } >> catch (PyException ex4) >> { System.err.println(ex4); >> } >> } >> >> The tests were made on Windows XP, using Java 6.0. >> >> 1) Am I doing something wrong? >> >> 2) Is there anything (a configuration switch?) that will force Jython >> to lazily import the modules when instantiated? >> >> Thanks. >> >> Marcel >> >> >> ------------------------------------------------------------------------------ >> _______________________________________________ >> Jython-users mailing list >> Jyt...@li... >> https://lists.sourceforge.net/lists/listinfo/jython-users >> > > > > -- > Jim Baker > jb...@zy... > |