How to Install Various Python Libraries in Jython

How can I install various Python libraries in Jython?

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'>
>>>

Pip install Installs in Jython instead of python

On your terminal/command prompt navigate to the Python home directory of the version for which you need to install the module. Then navigate into the scripts folder and run the pip command. Like below

cd C:/python27/Scripts
pip install XLRD

You basically need to point to the pip script for which you need to run this command. If thats for the pip script inside the python27, you need to navigate there or do like below as well

C:/python27/Scripts/pip install XLRD

If you have both the python27 & jython home directory & script paths in your 'PATH' environment variables, the path that was declared first takes precedence. To rearrange precedence, you would have to rearrange the order of the path declaration.

Jython and python modules

You embed jython and you will use some Python-Modules somewere:

if you want to set the path (sys.path) in your Java-Code :

public void init() {
interp = new PythonInterpreter(null, new PySystemState());

PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(rootPath));
sys.path.append(new PyString(modulesDir));
}

Py is in org.python.core.

rootPath and modulesDir is where YOU want !

let rootPath point where you located the standard-jython-lib

Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example

Importing python modules in jython

You've done the right thing printing sys.path. Now you should go to your python shell, and do this:

$ python
>>> import scapy
>>> print scapy.__file__

This will show where scapy is being imported from. The most likely problem is that your jython sys.path doesn't include the directory containing scapy.

How can I install various Python libraries in Jython?

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'>
>>>


Related Topics



Leave a reply



Submit