Python project dependencies with Maven - ModuleNotFoundError: No module named 'xxx'
Python project dependencies with Maven - ModuleNotFoundError: No module named 'xxx'
我正在为多个相互依赖的 Python 项目进行基本设置(我使用的是 Python 3.6)。后面还会涉及Java,所以我选择了Maven来进行构建管理。到目前为止,我有一个 parent-pom、一个 common 和一个 few specific projects.
一般的文件夹结构是:
父 pom
|- pom.xml
普通
|- src/main/python/xxx/yyy/common
|- Utils.py
|- 子文件夹 1
|- 子文件夹 2
|- …
|- pom.xml
项目 1
|- src/main/python/xxx/yyy/project1
|- 子文件夹 A
|- 子文件夹 B
|- …
|- pom.xml
实际的 POM 文件如下所示:
parent-pom 项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxx.yyy</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>project1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
普通项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>aaa</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
project1 项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>project1</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>xxx.yyy</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<configuration>
<executable>python</executable>
<workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
<arguments>
<argument>Launcher.py</argument>
</arguments>
</configuration>
<id>python-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
现在我在 project1 中有一个名为 Launcher
的 class,它应该导入 class Utils
来自 common 包:
Launcher.py
from xxx.yyy.common.Utils import Utils
class Launcher:
def __init__(self):
Utils.doSomething()
if ('__main__' == __name__):
Launcher()
Utils.py
class Utils:
@staticmethod
def doSomething():
print ("Test")
但是,导入
from xxx.yyy.common.Utils import Utils
无法解决:
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ project1 ---
[INFO] Installing C:\...\project1\pom.xml to C:\...\.m2\repository\xxx\yyy\project1[=16=].0.1-SNAPSHOT\project1-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.949 s
[INFO] Finished at: 2019-04-24T12:19:16+02:00
[INFO] ------------------------------------------------------------------------
C:\...\project1>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
Traceback (most recent call last):
File "Launcher.py", line 1, in <module>
from xxx.yyy.common.Utils import Utils
ModuleNotFoundError: No module named 'xxx'
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:804)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:751)
at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:313)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.787 s
[INFO] Finished at: 2019-04-24T12:19:24+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (python-build) on project project1: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
我想,为了访问不同 Python/Maven 项目中的模块和包,声明 POM 依赖关系是不够的。
有没有办法在 POM 文件中解决这个问题?
@khmarbaise:问题是如何通过将 Python 项目添加到 POM 文件中的 PYTHONPATH 来解决 ModuleNotFoundError
。对不起,我应该问得更清楚。
但是,我找到了解决这个问题的方法。我只需要在 Python 执行部分的配置中将 PYTHONPATH 路径添加到 environmentVariables
标记 ...
解法:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<configuration>
<executable>python</executable>
<workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
<arguments>
<argument>Launcher.py</argument>
</arguments>
<environmentVariables>
<PYTHONPATH>
${project.basedir.parent}/common/src/main/python/ <!-- added PYTHONPATH -->
</PYTHONPATH>
</environmentVariables>
</configuration>
<id>python-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
我正在为多个相互依赖的 Python 项目进行基本设置(我使用的是 Python 3.6)。后面还会涉及Java,所以我选择了Maven来进行构建管理。到目前为止,我有一个 parent-pom、一个 common 和一个 few specific projects.
一般的文件夹结构是:
父 pom
|- pom.xml
普通
|- src/main/python/xxx/yyy/common
|- Utils.py
|- 子文件夹 1
|- 子文件夹 2
|- …
|- pom.xml
项目 1
|- src/main/python/xxx/yyy/project1
|- 子文件夹 A
|- 子文件夹 B
|- …
|- pom.xml
实际的 POM 文件如下所示:
parent-pom 项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxx.yyy</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>project1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
普通项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>aaa</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
project1 项目"pom.xml":
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>project1</artifactId>
<packaging>pom</packaging>
<parent>
<groupId>xxx.yyy</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>xxx.yyy</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<configuration>
<executable>python</executable>
<workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
<arguments>
<argument>Launcher.py</argument>
</arguments>
</configuration>
<id>python-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
现在我在 project1 中有一个名为 Launcher
的 class,它应该导入 class Utils
来自 common 包:
Launcher.py
from xxx.yyy.common.Utils import Utils
class Launcher:
def __init__(self):
Utils.doSomething()
if ('__main__' == __name__):
Launcher()
Utils.py
class Utils:
@staticmethod
def doSomething():
print ("Test")
但是,导入
from xxx.yyy.common.Utils import Utils
无法解决:
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ project1 ---
[INFO] Installing C:\...\project1\pom.xml to C:\...\.m2\repository\xxx\yyy\project1[=16=].0.1-SNAPSHOT\project1-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.949 s
[INFO] Finished at: 2019-04-24T12:19:16+02:00
[INFO] ------------------------------------------------------------------------
C:\...\project1>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< xxx.yyy:project1 >--------------------------
[INFO] Building project1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project1 ---
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (python-build) @ project1 ---
Traceback (most recent call last):
File "Launcher.py", line 1, in <module>
from xxx.yyy.common.Utils import Utils
ModuleNotFoundError: No module named 'xxx'
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:804)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:751)
at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:313)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.787 s
[INFO] Finished at: 2019-04-24T12:19:24+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (python-build) on project project1: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
我想,为了访问不同 Python/Maven 项目中的模块和包,声明 POM 依赖关系是不够的。
有没有办法在 POM 文件中解决这个问题?
@khmarbaise:问题是如何通过将 Python 项目添加到 POM 文件中的 PYTHONPATH 来解决 ModuleNotFoundError
。对不起,我应该问得更清楚。
但是,我找到了解决这个问题的方法。我只需要在 Python 执行部分的配置中将 PYTHONPATH 路径添加到 environmentVariables
标记 ...
解法:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<configuration>
<executable>python</executable>
<workingDirectory>src\main\python\xxx\yyy\project1\</workingDirectory>
<arguments>
<argument>Launcher.py</argument>
</arguments>
<environmentVariables>
<PYTHONPATH>
${project.basedir.parent}/common/src/main/python/ <!-- added PYTHONPATH -->
</PYTHONPATH>
</environmentVariables>
</configuration>
<id>python-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>