Skip to content

sys.executable generates error if a white space is present in the python executable path #119

Open
@AndreaBartolini

Description

@AndreaBartolini

Some month ago the use of sys.executable function has been introduced in the test.py script in order to get the current python executable path. I suppose that this has be done to make more general the script because some python installations have different commands to start them (e.g., python3 or similar).

This solution generates issues if some white spaces are present in the python path executable, for example on my windows machine the python executable path is the following:

C:\Program Files\Python311\python.exe

as, I think, in the most of the windows installations.

The output from the sys.executable function is the following:
'C:\\Program Files\\Python311\\python.exe'

By using this path the command generated to run the testmodel.py script is like the following:
cmd = C:\Program Files\Python311\python.exe testmodel.py --win --msysEnvironment=ucrt64 --libraries=C:\LibTest\master --ompython_omhome= STTlibrary_STTlibrary.Components.DigitalController.Examples.ControlledTank.conf.json > files/STTlibrary_STTlibrary.Components.DigitalController.Examples.ControlledTank.cmdout 2>&1, STTlibrary_STTlibrary.Components.DigitalController.Examples.ControlledTank, 300

that generates the following error on the subprocess call:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

That means that the shell does not resolve correctly the python executable path.

I tried to modify the python executable path as following:

'C:\\\"Program Files\"\\Python311\\python.exe'

that works fine if used in the win terminal as:
'C:\"Program Files"\Python311\python.exe

but in this case i get the following error

run: check_outputTraceback (most recent call last):
  File "C:\mnt\TempMyCI\LibCode\localopenmodelicalibrarytesting\OMLibraryTesting\test.py", line 184, in <module>
    check_output_log([pythonExecutable, "testmodel.py", "--help"], stderr=subprocess.STDOUT)
  File "C:\mnt\TempMyCI\LibCode\localopenmodelicalibrarytesting\OMLibraryTesting\test.py", line 101, in check_output_log
    return subprocess.check_output(*popenargs, **kwargs)

here below the popenargs and kwargs values:
(['C:\\"Program Files"\\Python311\\python.exe', 'testmodel.py', '--help'],) {'stderr': -2}

I tried with others combination of commas or backslash characters but the result doesn't change, so at the moment the unique solution that I can propose is the following change in the script

pythonExecutable = sys.executable
if not pythonExecutable or ' ' in pythonExecutable:
  pythonExecutable = "python"

that is not so elegant but works...

Any suggestion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions