1

I have a python script on a Windows Server called optimize.py that calls three other python scripts 4 times each using the os.system() method.

print('Creating input files for optimization algorithm...\n')

os.system("example.py 2 2 0 1")
os.system("example.py 3 3 0 1")
os.system("example.py 4 4 0 1")
os.system("example.py -1 10 0 1")

This works perfectly when I execute optimize.py myself. However, when it is executed from my ColdFusion application which is running on the same server, I get the error "Can't find a default Python" each time it tries to call example.py. The rest of the script runs just fine.

This is the ColdFusion code that is executing the python script:

<cf_exec cmd="cmd.exe">
    cd C:\xxx_Optimization\
    "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\bcp.exe" [SQL_Table] out [directory] -c -t, -U [user] -P [password] -S [server]
    cd C:\xxx_Optimization\pyOptimization\
    python C:\xxx_Optimization\pyOptimization\optimize.py
    <cfoutput>#pyUniqueZipCommand#</cfoutput>
    date /t > pyDone.txt
</cf_exec>

I've already added python to the PATH, and I've also followed the answers in this post, to no avail. Can anyone tell me what I'm doing wrong here, or suggest an alternative to os.system for executing these scripts?

EDIT: I've also tried specifying the entire filepath in the OS.system methods, but I received the same error.

HDuck
  • 395
  • 3
  • 16
  • 1
    What response do you get if you run `ftype Python.File` from your `cf_exec cmd.exe`? – Sev Roberts Mar 03 '21 at 18:39
  • It says `Python.file="C:\Windows\py.exe" "%L" %*` – HDuck Mar 03 '21 at 18:44
  • 1
    So the `Can't find a default Python` error is only being returned when trying to call further python scripts from within the initial python script? – Sev Roberts Mar 03 '21 at 18:54
  • 1
    I don't have a suitable environment to test this, but it sounds like you might need to use `os.system(sys.executable + "example.py 2 2 0 1")` or `os.system("python example.py 2 2 0 1")` – Sev Roberts Mar 03 '21 at 18:57
  • 1
    Yup that's correct, only when I'm trying to call a python script from within the initial script. Your suggestion seems to have solved the problem - I ended up going with `os.system("python example.py 2 2 0 1")`. If you copy that comment into an answer, I'll mark it as the correct one. Thanks a bunch! – HDuck Mar 03 '21 at 19:14

1 Answers1

2

If the Can't find a default Python error is only being returned when trying to call further python scripts from within the initial python script - ie if your ftype and PATH settings are known to be good - then the issue is with the new context, try:

os.system(sys.executable + "example.py 2 2 0 1")

or

os.system("python example.py 2 2 0 1")

Sev Roberts
  • 1,295
  • 6
  • 7