Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install various Python libraries in Jython?

I know that I can install Jython with Java and that I can use Jython where I use Python. The Jython shell is working fine.

In Jython, how can I install libraries like lxml, Scrappy and BeautifulSoup that I'd normally install via pip or easy_install

like image 466
Mirage Avatar asked Jul 22 '11 07:07

Mirage


People also ask

How do I import libraries into Jython?

Methods in this class can be called from the Jython script importex.py. Save and execute the above script from the command line to get following output. C:\jython27\bin>jython importex.py Hello World!

How do I install Jython modules?

Basic Install The installer will then walk through a similar set of steps in graphical or console mode: showing the license, selecting an install directory and JVM and actually copying Jython to the filesystem. After this completes, Jython is installed in the directory you selected.


2 Answers

Some Python modules, like lxml, have required components in C. These won't work in Jython.

Most Python packages will work fine, and you can install them using the same tools as you use in CPython. This is described in Appendix A of Jython Book:

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:

$ jython ez_setup.py 

[The easy_install script will be] 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.

Testing it myself, after installing setuptools in Jython, pip installed correctly:

$ sudo /usr/bin/jython2.5.2b1/bin/easy_install pip Searching for pip [...] Installing pip-2.5 script to /usr/bin/jython2.5.2b1/bin Installing pip script to /usr/bin/jython2.5.2b1/bin  Installed /usr/bin/jython2.5.2b1/Lib/site-packages/pip-1.0.2-py2.5.egg Processing dependencies for pip Finished processing dependencies for pip  $ sudo /usr/bin/jython2.5.2b1/bin/pip install bottle Downloading/unpacking bottle   Downloading bottle-0.9.6.tar.gz (45Kb): 45Kb downloaded   Running setup.py egg_info for package bottle     Installing collected packages: bottle   Running setup.py install for bottle Successfully installed bottle Cleaning up...  $ jython Jython 2.5.2b1 (Release_2_5_2beta1:7075, Jun 28 2010, 07:44:20)  [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_26 Type "help", "copyright", "credits" or "license" for more information. >>> import bottle >>> bottle <module 'bottle' from '/usr/bin/jython2.5.2b1/Lib/site-packages/bottle$py.class'> >>> 
like image 115
Jeremy Avatar answered Oct 09 '22 03:10

Jeremy


As of v2.7b4, the Jython distribution includes the ensurepip module, which simplifies installation of pip and setuptools:

jython -m ensurepip 

Beware of sys.platform=='win32' issue that will get in your way of using PyPI packages which relying on this method to determine host platform.

like image 30
Each Avatar answered Oct 09 '22 04:10

Each