Using Other Tools with Jython
Total Page:16
File Type:pdf, Size:1020Kb
A P P E N D I X A Using Other Tools with Jython The primary focus of this appendix is to provide information on using some external Python packages with Jython, as well as providing information regarding the Jython registry. In some circumstances, the tools must be used or installed a bit differently on Jython than on CPython, and those differences will be noted. Because there is a good deal of documentation on the usage of these tools available on the web, this appendix will focus on using the tool specifically with Jython. However, relevant URLs will be cited for finding more documentation on each of the topics. The Jython Registry Because there is no good platform-independent equivalent of the Windows Registry or Unix environment variables, Java has its own environment variable namespace. Jython acquires its namespace from the following sources (later sources override defaults found in earlier places): • The Java system properties, typically passed in on the command line as options to the java interpreter. • The Jython “registry” file, containing prop=value pairs. Read on for the algorithm Jython uses to find the registry file. • The user’s personal registry file, containing similarly formatted prop/value pairs. The user’s registry file can be found at "user.home"+"/.jython" • Jython properties specified on the command line as options to the Jython class. See the -D option to the interpreter. Registry Properties The following properties are recognized by Jython. There may be others that aren’t documented here; consult the comments in registry file for details. python.cachedir The directory to use for caches—currently just package information. This directory must be writable by the user. If the directory is an absolute path, it is used as given; otherwise, it is interpreted as relative to sys.prefix. python.verbose Sets the verbosity level for informative messages. Valid values in order of increasing verbosity are “error,” “warning,” “message,” “comment,” and “debug.” 437 APPENDIX A USING OTHER TOOLS WITH JYTHON python.security.respectJavaAccessibility Normally, Jython can only provide access to public members of classes. However if this property is set to false and you are using Java 1.2, then Jython can access nonpublic fields, methods, and constructors. python.jythonc.compiler The Java compiler to use with the JythonC tool, which now generates Java source code. This should be the absolute path to a Java compiler, or the name of a compiler on your standard PATH. python.jythonc.classpath Extensions to the standard java.class.path property for use with JythonC. This is useful if you use Jikes as your compiler. python.jythonc.compileropts Options to pass to the Java compiler when using JythonC. python.console The name of a console class. An alternative console class that supports GNU readline can be installed with this property. Jython already includes such a console class, and it can be enabled by setting this property to org.python.util.ReadlineConsole. python.console.readlinelib Allow a choice of backing implementation for GNU readline support. Can be either GnuReadline or Editline. This property is only used when python.console is set to org.python.util.ReadlineConsole. Finding the Registry File To find the Jython registry file and set the Python values for sys.prefix, you must first locate a root directory. If a “python.home” exists, it is used as the root directory by default. If “python.home” does not exist, “install.root” is used. If neither of these exists, then Jython searches for the file “jython.jar” on the Java classpath, as defined in the system property java.class.path. (The actual file system isn’t searched, only the paths defined on the classpath and one of them must literally include “jython.jar”). Once the root directory is found, sys.prefix and sys.exec_prefix are set to it, and sys.path has rootdir/Lib appended to it. The registry file used is then rootdir/registry. Setuptools Setuptools is a library that builds upon distutils, the standard Python distribution facility. It offers some advanced tools like easy_install, a command to automatically download and install a given Python package and its dependencies. To get setuptools, download ez_setup.py from http://peak.telecommunity.com/dist/ez_setup.py. Then, go to the directory where you left the downloaded file and execute: 438 APPENDIX A USING OTHER TOOLS WITH JYTHON $ jython ez_setup.py The output will be similar to the following: Downloading http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg Processing setuptools-0.6c9-py2.5.egg Copying setuptools-0.6c9-py2.5.egg to /home/lsoto/jython2.5.0/Lib/site-packages Adding setuptools 0.6c9 to easy-install.pth file Installing easy_install script to /home/lsoto/jython2.5.0/bin Installing easy_install-2.5 script to /home/lsoto/jython2.5.0/bin Installed /home/lsoto/jython2.5.0/Lib/site-packages/setuptools-0.6c9-py2.5.egg Processing dependencies for setuptools==0.6c9 Finished processing dependencies for setuptools==0.6c9 As you can read on the output, the easy_install script has been installed to the bin directory of the Jython installation (/home/lsoto/jython2.5.0/bin in the example above). If you work frequently with Jython, it’s a good idea to prepend this directory to the PATH environment variable, so you don’t have to type the whole path each time you want to use easy_install or other scripts installed to this directory. From now on, we’ll assume that this is the case. If you don’t want to prepend Jython’s bin directory to your PATH for any reason, remember to type the complete path on each example (i.e., type /path/to/jython/bin/easy_install when I say easy_install). Okay, so now you have easy_install. What’s next? Let’s grab a Python library with it! For example, let’s say that we need to access Twitter from a program written in Jython, and we want to use python- twitter project, located at http://code.google.com/p/python-twitter/. Without easy_install, you would go to that URL, read the building instructions and, after downloading the latest version and executing a few commands, you should be ready to go. Except that libraries often depend on other libraries (as the case with python-twitter which depends on simplejson) so you would have to repeat this boring process a few times. With easy_install you simply run: $ easy_install python-twitter And you get the following output: Searching for python-twitter Reading http://pypi.python.org/simple/python-twitter/ Reading http://code.google.com/p/python-twitter/ Best match: python-twitter 0.6 Downloading http://python-twitter.googlecode.com/files/python-twitter-0.6.tar.gz Processing python-twitter-0.6.tar.gz Running python-twitter-0.6/setup.py -q bdist_egg --dist-dir /var/folders/mQ/mQkMNKiaE583pWpee85FFk+++TI/-Tmp-/easy_install-FU5COZ/python-twitter- 0.6/egg-dist-tmp-EeR4RD zip_safe flag not set; analyzing archive contents... Unable to analyze compiled code on this platform. Please ask the author to include a 'zip_safe' setting (either True or False) in the package's setup.py Adding python-twitter 0.6 to easy-install.pth file Installed /home/lsoto/jython2.5.0/Lib/site-packages/python_twitter-0.6-py2.5.egg Processing dependencies for python-twitter Searching for simplejson 439 APPENDIX A USING OTHER TOOLS WITH JYTHON Reading http://pypi.python.org/simple/simplejson/ Reading http://undefined.org/python/#simplejson Best match: simplejson 2.0.9 Downloading http://pypi.python.org/packages/source/s/simplejson/simplejson- 2.0.9.tar.gz#md5=af5e67a39ca3408563411d357e6d5e47 Processing simplejson-2.0.9.tar.gz Running simplejson-2.0.9/setup.py -q bdist_egg --dist-dir /var/folders/mQ/mQkMNKiaE583pWpee85FFk+++TI/-Tmp-/easy_install-VgAKxa/simplejson-2.0.9/egg- dist-tmp-jcntqu *************************************************************************** WARNING: The C extension could not be compiled, speedups are not enabled. Failure information, if any, is above. I'm retrying the build without the C extension now. *************************************************************************** *************************************************************************** WARNING: The C extension could not be compiled, speedups are not enabled. Plain-Python installation succeeded. *************************************************************************** Adding simplejson 2.0.9 to easy-install.pth file Installed /home/lsoto/jython2.5.0/Lib/site-packages/simplejson-2.0.9-py2.5.egg Finished processing dependencies for python-twitter The output is a bit verbose, but it gives you a detailed idea of the steps automated by easy_install. Let’s review it piece by piece: Searching for python-twitter Reading http://pypi.python.org/simple/python-twitter/ Reading http://code.google.com/p/python-twitter/ Best match: python-twitter 0.6 Downloading http://python-twitter.googlecode.com/files/python-twitter-0.6.tar.gz We asked for “python-twitter,” which is looked up on PyPI, the Python Package Index, which lists all the Python packages produced by the community (as long as they have been registered by the author, which is usually the case). The version 0.6 was selected since it was the most recent version at the time we ran the command. Let’s see what's next on the easy_install output: Running python-twitter-0.6/setup.py -q bdist_egg --dist-dir /var/folders/mQ/mQkMNKiaE583pWpee85FFk+++TI/-Tmp-/easy_install-FU5COZ/python-twitter- 0.6/egg-dist-tmp-EeR4RD zip_safe flag not set; analyzing archive contents... Unable to analyze compiled code on this platform. Please ask the author to include a 'zip_safe' setting (either True or False) in the package's setup.py Adding python-twitter 0.6 to easy-install.pth file Installed /home/lsoto/jython2.5.0/Lib/site-packages/python_twitter-0.6-py2.5.egg Nothing special here: it ran the needed commands to install the library.