0

I am trying to invoke Visual Studio build from a python program. My code given below. It shows the error.

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

vspath = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE";                      
devenvpath = os.path.join(vspath, "devenv.exe")     
buildcmd =  '\"' + devenvpath + '\" ' + 'Test.sln /Rebuild \"Release|Any CPU\" /project Test'
print(buildcmd)
os.system(buildcmd)
FaisalM
  • 724
  • 5
  • 18
  • 1
    That looks like the error you'd get from pasting the executable name into the command line without quotes – Mad Physicist Aug 30 '19 at 12:07
  • What does the `print(buildcmd)` command print? –  Aug 30 '19 at 12:08
  • @Sembei "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe" Test.sln /Rebuild "Release|Any CPU" /project Test – FaisalM Aug 30 '19 at 12:25

2 Answers2

1

os.system() is frowned upon since several years. (Or is it already deprecated?)

Instead, use subprocess.

In your case,

vspath = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\Common7\\IDE"
devenvpath = os.path.join(vspath, "devenv.exe")     
buildcmd = [devenvpath, "Test.sln",  "/Rebuild", "Release|Any CPU", "/project", "Test"]
print(buildcmd)
subprocess.Popen(buildcmd)

should do it.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • I already tried that. with `Popen` no error is shown. However the executable is not started. – FaisalM Aug 30 '19 at 12:37
  • Examining the task-manager, I see the exe is started, but the output from the exe is not shown in the command line. Should I do anything special for that? – FaisalM Aug 30 '19 at 13:00
  • @FaisalM Strange, normally the output of that command should appear on the current console (if it is a console program. Is it one?). – glglgl Aug 30 '19 at 13:04
  • Yes. I am invoking this python script from console. – FaisalM Aug 30 '19 at 13:19
  • @FaisalM That wasn't the question. You can start non-console programs from the console, these won't produce output. Are you sure this program produces output to its stdout? Did you call it manually? – glglgl Aug 30 '19 at 13:40
  • Yes. Running the exe directly from cmd shows the outputs. From [this](https://stackoverflow.com/questions/1388753/how-to-get-output-from-subprocess-popen-proc-stdout-readline-blocks-no-dat,) I understand that I have to read from the process output. – FaisalM Aug 30 '19 at 13:45
  • @FaisalM You can do so if you specify `stdout=subprocess.PIPE`, but you just can have the outputs be put to the terminal. Why this doesn't happen in your case, I don't know. – glglgl Aug 30 '19 at 13:51
0

This works just fine for me using Python3 and Windows10

import os
print(os.listdir("C:\\Program Files (x86)\\Windows NT"))

output is

['Accessories', 'TableTextService']

At what line are you getting an error? If the problem truly was with the path you would get an error when calling os.path.join. If you're getting an error on os.system then try using subprocess instead

import subprocess
subprocess.check_output("your command goes here",shell=True)
Calculus
  • 781
  • 7
  • 20
  • This works fine when I just invoke the executable. When the command line arguments are passed, I get this error. – FaisalM Aug 30 '19 at 12:26