Jython:java third-party package path issue for calling Python files

Source: Internet
Author: User

This method solves the portability of Python code, does not need to configure the Python environment on the new machine, and embeds Python code in Java Engineering only by installing Jython

1. How Jython Installs

: Jython_installer-2.5.0.jar. Need to download points, if no points, you can contact the landlord.

The next way to install a fool, the path is best and the project is selected in the same directory.

2. Run Python code

For a simple example, after installing the Java environment and Eclipse, copy the following code to run. The comment section is how you run the file, and you need to remove the comment execution after you create the file.

Import Java.io.ioexception;import org.python.util.PythonInterpreter;  public class Test {public  static void Main (String args[]) throws IOException {  Pythoninterpreter interpreter = NE W Pythoninterpreter (); Run the Python statement interpreter.exec ("a = \" Hello, jython\ "");  Interpreter.exec ("Print a");  Execute Python File//  interpreter.exec ("filepy = \" E:\\test.py\ "");  Interpreter.execfile (filepy);  Execute python py file  //  filepy.close ();}}  
3. Handling third-party packages

In the way the file is run,

The first case: if the. py file contains a custom package of the same directory, it can be called normally under Eclipse engineering.

The second case: if a third-party package is included, it needs to be copied to the current directory.

Third case: cannot be called because of a path problem. There are a number of third-party packages under the Jython/lib file, such as OS, RE, __future__, and repetitive copying, which can be manually modified by the path path. (recommended)

There are two ways of modifying a path:

(1) Java mode

Import Org.python.core.py;import org.python.core.PySystemState; System.out.println (Sys.path.toString ());    Previouspysystemstate sys = py.getsystemstate ();   Sys.path.add ("E:\\sacaapm-paserver\\src-python\\jython\\lib"); System.out.println (Sys.path.toString ());   Later

Embed the code in the Part 2 code and execute it after the complete code, and you can see the changes in the front and back paths.

(2) Python mode

Interpreter.exec ("Import sys");  Interpreter.exec ("Print Sys.path");

(3) Flexible use of the above two methods or their combined form.

When the import sys times out "SYS module does not exist" error, it is recommended to use the first type.

The complete code is as follows:

Import Java.io.fileinputstream;import java.io.ioexception;import java.io.inputstream;import org.python.core.Py; Import Org.python.core.pysystemstate;import org.python.util.PythonInterpreter;  public class Test {public  static void Main (String args[]) throws IOException {    Pythoninterpreter interpreter = NE W Pythoninterpreter ();    Pysystemstate sys = py.getsystemstate ();     Sys.path.add ("E:\\src-python\\jython\\lib");       Interpreter.exec ("Import sys");   Interpreter.exec ("Print Sys.path");  Interpreter.exec ("path = \" E:\\src-python\\jython\\lib\ "");  Interpreter.exec ("Sys.path.append (Path)");   Interpreter.exec ("Print Sys.path");    Interpreter.exec ("A=3; b=5; ");  InputStream filepy = new FileInputStream ("e:\\input.py");     Interpreter.execfile (filepy);  Filepy.close ();     }}
Operation Result:
[' E:\\lib\\lib ', ' __classpath__ ', ' __pyclasspath__/', U ' e:\\src-python\\jython\\lib '] [' E:\\lib\\lib ', ' __classpath__ ', ' __pyclasspath__/', U ' e:\\src-python\\jython\\lib ', ' e:\\src-python\\jython\\ Lib ']a/b= 0.6

e:\\input.py File Code:

From __future__ import Divisionprint "a/b=", A/b

Where the value of variable A, B is passed in by Java.

Jython:java third-party package path issue for calling Python files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.