Debian Codegraph @

doc/install.rst

raw
1 ############
2 Installation
3 ############
4
5
6 jpy's installation is currently the full build process from sources.
7 We will try to ease the installation process in the future.
8
9 After successful installation you will be able
10
11 * to use Java from Python by importing the jpy module ``import jpy`` and
12 * to use Python from Java by importing the jpy Java API classes ``import org.jpy.*;`` from ``jpy.jar`` on your Java classpath.
13
14 *******************
15 Getting the Sources
16 *******************
17
18 The first step is to clone the jpy repository or download the sources from the `jpy Project page <https://github.com/jpy-consortium/jpy>`_.
19 We recommend you clone the repository using the `git <http://git-scm.com/>`_ tool::
20
21 git clone https://github.com/jpy-consortium/jpy.git
22
23 If you don't want to use git, you can also download stable source releases from the
24 `jpy releases page <https://github.com/jpy-consortium/jpy/releases>`_ on GitHub.
25
26 In the following it is assumed that the jpy sources are either checked out or unpacked into a directory named ``jpy``.
27
28 .. _build:
29
30 ******************
31 Build from Sources
32 ******************
33
34 Change into the checkout directory (``cd jpy``) and follow the build steps below. After successful build, the
35 ``build`` directory will contain the platform-dependent jpy versions:
36
37 build/
38 lib-*os-platform*-*python-version*/
39 jpy.so (Unixes only)
40 jdl.so (Unixes only)
41 jpy.pyd (Windows only)
42 jdl.pyd (Windows only)
43 jpyutil.py
44 jpyconfig.py
45 jpyconfig.properties
46
47
48 ************************
49 Running Java from Python
50 ************************
51
52 In order to use jpy from Python you will have to add the respective directory ``lib-``*os-platform*``-``*python-version*
53 for your platform and Python version to your Python path. This can be done either programmatically in Python, e.g.
54
55 import sys
56 sys.path.append('build/lib-*os-platform*-*python-version*')
57
58 You could also alter the ``PYTHONPATH`` environment variable,
59
60 export PYTHONPATH=$PYTHONPATH:build/lib-*os-platform*-*python-version*
61
62 Finally you could copy the contained files into your Python
63 installation's ``site-packages`` directory to make jpy permanently available.
64
65 ************************
66 Running Python from Java
67 ************************
68
69 To use the jpy Java API, put ``lib/jpy.jar`` on your classpath. Or if you use Maven add the following dependency to your
70 project:
71
72 <dependency>
73 <groupId>org.jpy</groupId>
74 <artifactId>jpy</artifactId>
75 <version>0.8</version>
76 </dependency>
77
78 The jpy Java API requires a maximum of two configuration parameters:
79
80 * ``jpy.jpyLib`` - path to the 'jpy' Python module, namely the ``jpy.so`` (Unix) or ``jpy.pyd`` (Windows) file
81 * ``jpy.jdlLib`` - path to the 'jdl' Python module, namely the ``jpy.so`` (Unix) file. Not used on Windows.
82
83 Another optional parameter
84
85 * ``jpy.debug`` - which is either ``true`` or ``false`` can be used to output extra debugging information.
86
87 All the parameters can be passed directly to the JVM either as Java system properties or by using the single system property
88
89 * ``jpy.config`` - which is a path to a Java properties files containing the definitions of the two parameters named above.
90
91 Such property file is also written for each build and is found in ``build/lib-<os-platform>-<python-version>/jpyconfig.properties``.
92
93 Setting PYTHONHOME
94 ------------------
95
96 If the environment variable ``PYTHONHOME`` is not set when you call Python from Java, you may get an error about
97 file system encodings not being found. It is possible to set the location of Python from your
98 Java program. Use ``PyLib.setPythonHome(pathToPythonHome)`` to do that, where ``pathToPythonHome`` is a ``String`` that
99 contains the location of the Python installation.
100
101 ========================
102 Build for Linux / Darwin
103 ========================
104
105 You will need
106
107 * `Python 2.7 or 3.3 <http://www.python.org/>`_ or higher
108 * `Oracle JDK 7 <http://www.oracle.com/technetwork/java/javase/downloads/>`_ or higher
109 * `Maven 3 <http://maven.apache.org/>`_ or higher
110 * For Linux: ``gcc``
111 * For Darwin: `Xcode <https://itunes.apple.com/de/app/xcode/id497799835?mt=12>`_
112
113 To build and test the jpy Python module use the following commands::
114
115 export JDK_HOME=<path to the JDK installation directory>
116 export JAVA_HOME=$JDK_HOME
117 python setup.py --maven build
118
119 where ``JAVA_HOME`` is used by Maven and ``JDK_HOME`` by ``setup.py``.
120 On Darwin, you may find the current JDK/Java home using the following expression::
121
122 export JDK_HOME=$(/usr/libexec/java_home)
123
124 If you encounter linkage errors during setup saying that something like a ``libjvm.so`` (Linux) or ``libjvm.dylib``
125 (Darwin) cannot be found, then you can try adding its containing directory to the ``LD_LIBRARY_PATH`` environment
126 variable, e.g.::
127
128 export LD_LIBRARY_PATH=$JDK_HOME/jre/lib/server:$LD_LIBRARY_PATH
129
130
131 ===========================
132 Build for Microsoft Windows
133 ===========================
134
135 Python 2.7
136 ----------
137
138 You will need
139
140 * `Python 2.7 <http://www.python.org/>`_ or higher (2.6 may work as well but is not tested)
141 * `Oracle JDK 7 <http://www.oracle.com/technetwork/java/javase/downloads/>`_ or higher (JDK 6 may work as well)
142 * `Maven 3 <http://maven.apache.org/>`_ or higher
143 * `Microsoft Visual C++ 10 <http://www.microsoft.com/en-us/download/details.aspx?id=8279>`_ or higher
144
145 Note that if you build for a 32-bit Python, make sure to also install a 32-bit JDK. Accordingly, for a 64-bit Python,
146 you will need a 64-bit JDK. If you use the free Microsoft Visual C++ Express edition, then you only can build for
147 a 32-bit Python.
148
149 Open the command-line and execute::
150
151 SET VS90COMNTOOLS=%VS100COMNTOOLS%
152 SET JDK_HOME=<path to the JDK installation directory>
153 SET JAVA_HOME=%JDK_HOME%
154 SET PATH=%JDK_HOME%\jre\bin\server;%PATH%
155
156 Then, to actually build and test the jpy Python module use the following command::
157
158 python setup.py --maven build
159
160
161 Python 3.3 and higher
162 ---------------------
163
164 You will need
165
166 * `Python 3.3 <http://www.python.org/>`_ or higher (3.2 may work as well but is not tested)
167 * `Oracle JDK 7 <http://www.oracle.com/technetwork/java/javase/downloads/>`_ or higher (JDK 6 may work as well)
168 * `Maven 3 <http://maven.apache.org/>`_ or higher
169 * `Microsoft Windows SDK 7.1 <http://www.microsoft.com/en-us/download/details.aspx?id=8279>`_ or higher
170
171 If you build for a 32-bit Python, make sure to also install a 32-bit JDK. Accordingly, for a 64-bit Python, you will
172 need a 64-bit JDK.
173
174 The Python setup tools (``distutils``) can make use of the command-line C/C++ compilers of the free Microsoft Windows SDK.
175 These will by used by ``distutils`` if the ``DISTUTILS_USE_SDK`` environment variable is set. The compilers are made accessible via
176 the command-line by using the ``setenv`` tool of the Windows SDK. In order to install the Windows SDK do the following
177
178 1. If you already use Microsoft Visual C++ 2010, make sure to uninstall the x86 and amd64 compiler redistributables first. Otherwise the installation of the Windows SDK will definitely fail. This may also apply to higher versions of Visual C++.
179 2. Download and install `Windows SDK 7.1 <http://www.microsoft.com/en-us/download/details.aspx?id=8279>`_. (This step failed for me the first time. A second 'repair' install was successful.)
180 3. Download and install `Windows SDK 7.1 SP1 <http://www.microsoft.com/en-us/download/details.aspx?id=4422>`_.
181
182 Open the command-line and execute::
183
184 "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\setenv" /x64 /release
185
186 to prepare a build of the 64-bit version of jpy. Use::
187
188 "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\setenv" /x86 /release
189
190 to prepare a build of the 32-bit version of jpy. Now set other environment variables::
191
192 SET DISTUTILS_USE_SDK=1
193 SET JDK_HOME=<path to the JDK installation directory>
194 SET JAVA_HOME=%JDK_HOME%
195 SET PATH=%JDK_HOME%\jre\bin\server;%PATH%
196
197 Then, to actually build and test the jpy Python module use the following command::
198
199 python setup.py --maven build
200
201
202
203
204 **********************
205 Typical Build Problems
206 **********************
207
208 =====================
209 Environment variables
210 =====================
211
212 Make sure that ``JAVA_HOME`` and ``JDK_HOME`` are always set, not only when installing, but also when using jpy. Additionally make sure that your ``PATH`` environment variable contains the ``JAVA_HOME``.
213
214 Set environment variables on `Windows <http://www.computerhope.com/issues/ch000549.htm>`_
215
216 Set environment variables on `Linux <http://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables>`_
217
218 ==============================================
219 Binary incompatibility between Python and Java
220 ==============================================
221
222 When used from Python, jpy must be able to find an installed Java Virtual Machine (JVM) on your computer. This is
223 usually the one that has been linked to the Python module during the build process.
224
225 If the JVM cannot be found, you will have to adapt the ``LD_LIBRARY_PATH`` (Unix) or ``PATH`` (Windows) environment
226 variables to contain the path to the JVM shared libraries. That is ``libjvm.dylib`` (Darwin), ``libjvm.so`` (Linux) and
227 ``jvm.dll`` (Windows). Make sure to use matching platform architectures, e.g. only use a 64-bit JVM for a 64-bit Python.
228
229 Otherwise the JVM may be found but you will get error similar to the following one (Windows in this case)::
230
231 >>> import jpy
232 Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Python33-amd64\Lib\site-packages\jpy.pyd: Can't load AMD 64-bit .dll on a IA 32-bit platform
233
234
235 ======================================
236 Unable to find vcvarsall.bat (Windows)
237 ======================================
238
239 If you build for Python 2.7, ``setup.py`` may fail with the following message::
240
241 C:\Users\Norman\JavaProjects\jpy>c:\Python27-amd64\python.exe setup.py install
242 Building a 64-bit library for a Windows system
243 running install
244 running build
245 running build_ext
246 building 'jpy' extension
247 error: Unable to find vcvarsall.bat
248
249 This happens, because ``distutils`` uses an environment variable of an older Microsoft Visual C++ version,
250 namely ``VS90COMNTOOLS``. Make sure to it to the value of your current version. For example::
251
252 SET VS90COMNTOOLS=%VS100COMNTOOLS%
253
254
255 =========================
256 DLL load failed (Windows)
257 =========================
258
259 ``setup.py`` may fail with the following message::
260
261 C:\Users\Norman\JavaProjects\jpy>c:\Python27\python.exe setup.py install
262 Building a 32-bit library for a Windows system
263 running install
264 running build
265 running build_ext
266 ...
267 running install_lib
268 running install_egg_info
269 Removing c:\Python27\Lib\site-packages\jpy-0.7.2-py2.7.egg-info
270 Writing c:\Python27\Lib\site-packages\jpy-0.7.2-py2.7.egg-info
271 Importing module 'jpy' in order to retrieve its shared library location...
272 Traceback (most recent call last):
273 File "setup.py", line 133, in <module>
274 import jpy
275 ImportError: DLL load failed: %1 is not a valid Win32 application
276
277 Fix this by adding the path to the Java VM shared library (``jvm.dll``) to the ``PATH`` environment variable::
278
279 SET PATH=%JDK_HOME%\jre\bin\server;%PATH%
280