1

I have a command which I use for deployment from windows command line. Now I need to run the same from an external python3.4 script.

The command is C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\WebService.sln" /p:DeployOnBuild=true /p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml" /p:AllowUntrustedCertificate=true /p:UserName=name /p:Password=PASSWORD.

How can I achieve this. I tried subprocess . But it's not working.Please help me out.

2
  • I tried subprocess. But it's not working <- Exactly how? Show us exactly what you tried Commented Jun 4, 2015 at 7:25
  • @inspectorG4dget: I tried subprocess.call("C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\WebService.sln" /p:DeployOnBuild=true /p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml" /p:AllowUntrustedCertificate=true /p:UserName=name /p:Password=PASSWORD"). It gives me syntax error. Commented Jun 4, 2015 at 7:34

2 Answers 2

3

Your problems appear to be the \ and " characters, so use raw strings. Also, it is safer to use a list:

proc = subprocess.Popen(
            [r"C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild",
             r"D:\WebService\WebService.sln",
             r"/p:DeployOnBuild=true",
             r"/p:PublishProfile=D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml",
             r"/p:AllowUntrustedCertificate=true",
             r"/p:UserName=name",
             r"/p:Password=PASSWORD"])

proc.wait()

Strictly speaking you don't need raw strings for all those parameters, but it is safer to do so with Windows paths. You only need the internal double quotes if you have embedded whitespace (as the first parameter). Here we are not using a shell, set shell=True as a parameter if you need one. A reason to use a shell on Windows is for filename association, but you don't appear to be using that here.

Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to give relative path in this answer if I have the present working directory stored in a variable pwd
@NevinRaj: depends how you use it. Generally yes, all filenames can be relative and the current working directory is inherited (by default) by the child process, so you won't need pwd. However some programs require absolute paths, and often you cant be sure what the working directory will be, depending on the user.
Don't add quotes manually. subprocess.Popen calls subprocess.list2cmdline to handle this for you. This function assumes the argv parsing rules implemented by Microsoft's C runtime and CommandLineToArgvW. Most Windows programs adhere to these rules. If a program has its own rules for parsing the command line you'll have to pass the command as a string.
@eryksun: you are correct, but they are need if shell=True.
On Windows, Popen uses args = list2cmdline(args) even for a shell command, so just never add quotes manually with a list. That said, you shouldn't use a list for a shell command. On POSIX systems it's unlikely to be what you want (the parameters are passed to the shell itself, and used as $0, $1, etc). On Windows, cmd.exe has its own quirky rules for quoting and escaping, and Python doesn't currently have a function to implement cmd's 'logic' (tortured as it is).
|
0

Can you post some code with what you have tried so far?

The subprocess module should be able to handle that, with something like

theproc = subprocess.Popen(["COMMAND HERE"])
theproc.communicate()

or you could try with the shell flag

theproc = subprocess.Popen(["COMMAND HERE"], shell=True)

2 Comments

I tried theproc = subprocess.Popen([sys.executable, "C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\WebService.sln" /p:DeployOnBuild=true /p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml" /p:AllowUntrustedCertificate=true /p:UserName=Admin /p:Password=me@321"]) ^ SyntaxError: invalid syntax Process finished with exit code 1
Can you try theproc = subprocess.Popen("C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild \" D:\WebService\WebService.sln\" /p:DeployOnBuild=true /p:PublishProfile=\"D:\We bService\Properties\PublishProfiles\MyDeployment.pubxml\" /p:AllowUntrustedCer tificate=true /p:UserName=Admin /p:Password=me@321") theproc.communicate() Your command string has multiple double quotes, so the string ends before you want it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.