Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Maven to work with Python subprocess?

I am trying to write Python scripts to do some Maven commands automatically.

The following Python code does not work:

import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)

resulting in the following error:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    process = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

As a next step, I supply subprocess the full path to the mvn binary:

import subprocess
args = ['/usr/local/Cellar/maven/3.2.3/libexec/bin/mvn', '-version']
process = subprocess.Popen(args, stderr=subprocess.PIPE)
out, err = process.communicate()

This command gets a bit further, but printing err reveals the following complaint:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/libexec/java_home/bin/java

As a sanity check, I verify on my own terminal that JAVA_HOME is correctly set:

$ mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T16:58:10-04:00)
Maven home: /usr/local/Cellar/maven/3.2.3/libexec
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"

I am also able to run mvn without hassle on the terminal.

My question is: why is subprocess not able to run mvn, and how can I get it to do so?

like image 554
user3898238 Avatar asked Jan 09 '23 22:01

user3898238


1 Answers

Use

process = subprocess.Popen(args,shell=True)
like image 90
Sparagmos Avatar answered Jan 16 '23 22:01

Sparagmos