简体   繁体   中英

How to set environment variables and call a perl script with parameters in python subprocess.popen?

I'd like to have a method to do 3 things at the same time:

  1. Create a subprocess that run the vsvars32.bat (visual studio batch file)

  2. In this subprocess set the environment variables.For example in a cmd lines:

    • SET MYDIR = C:\\This\\this\\here
    • SET DIR = %MYDIR%
    • SET PATH = %DIR%\\bin;%PATH%
  3. Also into this subprocess call the perl script with his parameters. in cmd:

  • cd %MYDIR%\\SOURCE\\FILES
  • My_Perl.pl -Name Mac -owner -details -vs_version 2005 -Run_type rebuild

I created a code in python:

myenv = {'MYDIR' : 'C:\\This\\this\\here', 'DIR' : '%MYDIR%', 'PATH' : '%DIR%\\bin;%PATH%'}

batchCmd = 'c:/.../vsvars32.bat'

perlCmd = 'c:/.../MyPerl.pl'

perlValues = ['-Name', 'Mac', '-owner', '-details', '-vs_version', '2005', '-Run_type', 'rebuild']

process = subprocess.Popen(['cmd','/c', batchCmd ,'&&', perlCmd, perlValues], shell=False, stdin = subprocess.PIPE, stdout = subprocess.PIPE, env = myenv)

The problem is that the function subprocess.popen not recognize myenv values and the perlValues.

Try adding perl.exe to perl cmd.

perlCmd = 'C:\perl\perl.exe c:/.../MyPerl.pl'

Second, you use backslash for path in one place, slash in the other. That maybe a problem.

 myenv = {'MYDIR' : 'C:\This\this\here', using backslash
 '%DIR%\bin;%PATH%'} using slash

Simply try to print out the env, to see that the environment is populated or not:

 process = subprocess.Popen(['cmd','/c', 'set'], shell=False, stdin = subprocess.PIPE, stdout = subprocess.PIPE, env = myenv)

Also, you forgot to cd before executing perl script.

process = subprocess.Popen(['cmd','/c', batchCmd ,'&&', 'cd %MYDIR%\SOURCE\FILES', '&&', perlCmd, perlValues], shell=False, stdin = subprocess.PIPE, stdout = subprocess.PIPE, env = myenv)

regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM