简体   繁体   中英

How to import Python modules with Maven in a Jython project?

I'm writing a Jython project to create a text editor. The data model is written entirely in Python and uses external libraries like nose, jinja and ply. The GUI for the text editor uses Java Swing libraries. I'm trying to build this project with Maven, and found out about this useful plugin , which I've included in my pom file like so:

            <plugin>
                <groupId>net.sf.mavenjython</groupId>
                <artifactId>jython-compile-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jython</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <libraries>
                        <param>nose</param>
                        <param>ply</param>
                        <param>jinja</param>
                    </libraries>
                </configuration>
            </plugin>

When I mvn package the project, I get a successful build message and a jar, and all the Python modules are saved in a folder created by the Maven plugin above in target/classes/Lib. So far so good, but when I try to run the jar, I can't make my Python code find the Python libraries and get this error message:

java -jar target/text_editor-0.0.1-SNAPSHOT.jar
Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "<string>", line 2, in <module>
  File "__pyclasspath__/text_editor/main.py", line 13, in <module>
  File "__pyclasspath__/pymodel/pyfile.py", line 1, in <module>
  File "__pyclasspath__/pymodel/pyflex.py", line 1, in <module>
ImportError: No module named ply

Error line being import ply.lex as lex

Is there something left to do in my plugin configuration? Or something else in the POM file I need to add to build the linkage? I'm not a Maven expert, so I might have missed something that seems obvious to others. Is there any other way to import Python modules in a Jython project using Maven? Any pointers greatly appreciated :)

Found the solution to my problem! It was not Maven related at all, it could be sorted out by fixing the PYTHONPATH in the jar file. I suspected so, but thought it could be done via Maven. I was wrong, it needs to be done from the Java code in the Jython project. The main Java class should have something like this:

        PySystemState systemState = Py.getSystemState();

It was only a matter of adding the path to the libraries downloaded by my Maven jython plugin in target/classes/Lib in the systemState, like this:

        //Link some external python libraries installed via maven plugin 
        systemState.path.append(new PyString("target/classes/Lib"));

Libraries are found and things work. I'm happy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM