Search code examples
pythonjenkinsjenkins-pipelinewindows-11

Why can Jenkins not find python and how do I fix it?


Problem:

I have a Jenkins controller installed on a Windows 11 laptop, which somehow cannot execute Python scripts. Whether I use py or python it always outputs the same error message, which says that py / python cannot be found or is spelled incorrectly. This is roughly translated from German. I don't know the English message word for word. The actual error message is this:

Der Befehl "py" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

which is also peculiar, because German is only defined as the secondary language of this computer. But it might be because the initial setup was done in german, but I digress. This error message usually means that the path to the Python executable is missing or unknown, right?

Steps taken:

I have already tried the following:

  1. I checked if there is something wrong with my Jenkinsfile, however it would neither run the example pipeline from the official Jenkins documentation nor just python --version.

  2. I have tried various ways of calling the script in the Jenkinsfile, bat 'py ...', bat 'python ...', bat 'python3 ...' and none of them worked.

  3. I have checked if the $PATH variable has the Python path in it. It does.

  4. I have tried executing the Python script via cmd from the same location as Jenkins. It worked.

  5. I have installed the ShiningPanda plugin and added the path to the directory containing python.exe and py.exe. It still spat out the same error.

Additional info, which might be useful

I am executing this pipeline directly on the controller, and I am aware that this is not optimal. However this is just for testing purposes and a proof of concept.

Another thing that might have something to do with it is that I have installed Jenkins as LocalSystem, because my user account is not authorised to install it as a LocalService.

I can execute .bat files from the Jenkinsfile, so nothing is fundamentally wrong.


Solution

  • Turns out the Jenkins controller has its own PATH environment variable. Python was not added in mine, so in order to get it working I had to add the following to my Jenkinsfile. I put it before the stages:

    environment {
        PATH = "C:/path/to/dir/containing/python.exe;$PATH"
    }