0

This is the way i call a .py file within a folder from a .pyw file outside, i am able to do that successfully, but want to know how can i pass variable from this .pyw file to the .py file within a folder.

if int(verfyUser) == int(Username):
     path = self.application.applicationDirectory
     name = "User_UI"
     path = os.path.join(self.application.applicationDirectory, name)
     if os.path.exists(os.path.join(path, name + ".pyw")):
         filename =  os.path.join(path, name + ".pyw")
     else:
         filename = os.path.join(path, name + ".py")
     args = []
     if ' ' in filename:
        filename = '"' + filename + '"'
     python = sys.executable
     self.close()
     if ' ' in python:
          pythonQuoted = '"' + python + '"'
     else:
      pythonQuoted = python
     os.spawnv(os.P_NOWAIT, python, [pythonQuoted, filename] + args)

i tried to pass the variables in args[], but that's not the way. Please to help me. Want to find a way to pass the variable from .pyw to the calling py file. Hope i am clear.....

Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
Prasanth
  • 339
  • 1
  • 6
  • 19

1 Answers1

0

How do you want to pass the variables? Just the values? In that case your approach seems good: you can call the other Python script with the values as command-line arguments. Your other script should then parse the command-line arguments that it has received and utilize them in the code.

But, just to be sure, you cannot write variable = 3 in the current script, then spawn a separate Python script and then use variable right away somehow.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • i want to pass value, i tried to give it in arg=[], but getting this error >>> {'this': , 'target': < __main__.UserPOS_UI; proxy of >}. am i doing right, please help. – Prasanth Mar 29 '12 at 12:16
  • You can't pass a dictionary right away. But you can start a Python script with `python script.py 1 2 3` and then, in `script.py`, you can get the arguments using `import sys; print sys.argv`, which will give you `['script.py', '1', '2', '3']` in this case. You can then convert the arguments to whatever their type is. You can't pass complex objects just like that, unless you convert it to a string representation first and then construct it again (in the other Python script) using those values. – Simeon Visser Mar 29 '12 at 12:20