Dean Povey
Dean Povey

Reputation: 9446

How do I run python code from Maven where python may or may not be in the path

I want to run some Python unit tests from my Maven module. I have a setup with the exec plugin that works, however I have run into a snag. If Python is not in the path then this fails. Ideally I would like a way to modify the runtime path and add a known good hint to it, but find it in the runtime path otherwise.

This is my exec plugin setup

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <executable>python</executable>
                <workingDirectory>src/main/config/bus/pythonlib</workingDirectory>
                <arguments>
                  <argument>run_all_tests.py</argument>
                </arguments>
            </configuration>
            <id>python-test</id>
            <phase>test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Is there a better way to do this from Maven? Is there a way to substitute a variable for the python executable, or modify the path?

Upvotes: 3

Views: 2712

Answers (1)

Dean Povey
Dean Povey

Reputation: 9446

The only way I've come up with so far is to create a python.bat file in the working directory that add the hints to the path before running it. If python is in the path it will find it and execute it, otherwise it finds the bat script and then tries it with the amended path.

A hack, but it works, you put the hints in a PYTHON_HINTS environment variable

@echo off
rem Work around for developers whose setup does not have python in their path.
setlocal
set PATH=%PATH%;%PYTHON_HINTS%
python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

Upvotes: 4

Related Questions