Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variable as argument from 1 python script to another in Windows 7 environment
    text
    copied!<p>I am learning python and ran into a bit of an issue that I haven't been able to figure out how to do. To keep it simple, lets say I have 2 scripts: Main and CalledScript. I want Main to open an executable and get its pid, assign that to a variable and then open CalledScript.py as a subprocess and pass that variable to it as an argument. While I know that in most cases using import would be a better option, in my case I have to run it as a subprocess due to other parts of my project. Anyhow, I keep getting an error when I do this. It only happens when I try to send the pid. If I were to assign the variable "thePid" below with just some random number like "555" it would work just fine. CalledScript would receive it, print it to the screen, and all is well. But trying to assign it cproc.pid and send it doesn't go over well.</p> <p>Without further delay, here is a simple sample code:</p> <p><strong>Main.py</strong></p> <pre><code>from subprocess import Popen, PIPE import sys import subprocess import os cproc = Popen("C:\Test\Test.exe", stdin=PIPE, stdout=PIPE) thePid = cproc.pid theproc = subprocess.Popen(['C:\\CalledScript.py', thePid], shell=True) </code></pre> <p><strong>CalledScript.py</strong></p> <pre><code>import sys print "thePid is: %r" % sys.argv[1] </code></pre> <p>The error I am getting:</p> <pre><code>Traceback (most recent call last): File "main.py", line 12, in &lt;module&gt; theproc = subprocess.Popen(['C:\\CalledScript.py ', cproc.pid], shell=True) File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 852, in _execute_child args = list2cmdline(args) File "C:\Python27\lib\subprocess.py", line 587, in list2cmdline needquote = (" " in arg) or ("\t" in arg) or not arg TypeError: argument of type 'int' is not iterable </code></pre> <p>Any help would be outstanding! Sorry for the likely very obvious question. I searched around on google but didn't have much luck finding an answer! I just started messing with python the other day so I'm still learning!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload