53

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

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Mirage
  • 30,868
  • 62
  • 166
  • 261

4 Answers4

42

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'>
>>>
Jeremy
  • 1
  • 85
  • 340
  • 366
  • Thanks jeremy , that worked. Is it fine that if i have 2 sites running with normal `python django with mod_wsgi` and other sites with `jython django` on same server. Or i need to get separate server – Mirage Jul 22 '11 at 07:58
  • @user I don't use Django, so I can't tell you for sure, but I think you can probably just use a single server. – Jeremy Jul 22 '11 at 07:59
  • 1
    thanks for that. one thing more if i use pip with jython now is that `pip` same as `pip with python`. I mean do all those libraries which are available with `normal pip` can be used with `jython pip` or they have `separate libraries` – Mirage Jul 22 '11 at 08:02
  • @user It uses the same library of packages, the official [Python Package Index](http://pypi.python.org/). If Python's `pip` can find it, Jython's `pip` can as well. – Jeremy Jul 22 '11 at 08:04
  • Doesn't work for me, always results in -> TypeError: 'NoneType' object is unsubscriptable – TimothyP Dec 17 '12 at 12:41
  • Hello, can someone help me to get this to work? I tried running the command after the install completed. I see that the script files are in the bin, but I am unable to run them. I get an error "C:\jython2.5.3\bin>easy_install pip 'easy_install' is not recognized as an internal or external command, operable program or batch file." I am trying to do this on windows 7 jython 2.5.3 – pitchblack408 Aug 14 '14 at 07:31
  • This might be problematic on Windows and/or if you have both Python and Jython installed on same system? I got easy_install installed in Jython bin. But am have issues trying to install/use pip to then install bottle. Any tips on these 2 scenarios? – David Jan 18 '15 at 00:44
  • 3
    As of 2.7.0 Jython includes pip. – Prof. Falken Aug 21 '15 at 12:11
  • 1
    how do you invokepip? If jython includes pip, how do you actually use pip from jython? – Thufir Jan 16 '17 at 17:46
15

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.

Community
  • 1
  • 1
Each
  • 448
  • 3
  • 7
  • In the final release of Jython 2.7.0, the Jython installer by default runs `jython -m ensurepip` as the last install step for the standard install. It can also be selected for most of the other types of installs as well. Or run it after install. – Jim Baker May 27 '15 at 21:43
7

Being Jython though you have the power of the Java libraries, not the limitation of being unable to install a few python C libraries.

For example you would be better using Jsoup instead of Beautiful soup or go for a full solution like Jtidy.

Use Jaxp instead of lxml.

Also another option that fits your reuirements is NekoHTML

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
sayth
  • 6,696
  • 12
  • 58
  • 100
4

I have both CPython and Jython installed. Here is what I do if I want to install a package in Jython via pip.

jython -m pip install <package_name>

For example to install the robot framework in jython (as I want to write the keyword libraries in Java), I did

jython -m pip install robotframework
Moses
  • 611
  • 5
  • 20