3

I have one Python script. Let's call it controller.py. I'd like to use controller.py to run another Python script and pass several variables to it. Let's call the second script analyzer.py.

What is the best way to do this without importing analyzer.py as module? And how do I reference the variables I'm passing to analyzer.py within that script?

Here's my failed attempt using subprocess:

controller.py

import subprocess var1='mytxt' var2=100 var3=True var4=[['x','y','z'],['x','c','d']] var5=r"C:\\Users\\me\\file.txt" myargs=var1,var2,var3,var4,var5 my_lst_str = ' '.join(map(str, myargs)) my_lst_str ='python analyzer.py '+my_lst_str subprocess.call(my_lst_str,shell=True) 

analyzer.py

print 'Argument List:', str(sys.argv) 

I have looked through similar questions on Stack Overflow. One oft-recommended solution I tried was importing analyzer.py as a module, but analyzer.py defines many different functions. Using it as a module creates lots of nested functions, and managing the scope of variables within those nested functions was cumbersome.

I need to use Python 2 for these scripts. I'm on a Windows 10 machine.

12
  • 2
    Does this answer your question? What is the best way to call a script from another script? Commented May 16, 2020 at 20:22
  • 1
    I don't really understand how importing analyzer.py as a module can create nested functions. Nested functions are functions that are defined within another function. Maybe it's worth sharing some actual code here. If you insist on not importing analyzer.py, maybe run it as a separate python process with subprocess. Commented May 16, 2020 at 20:31
  • 2
    Importing modules is normal, it shouldn't be cumbersome. Commented May 16, 2020 at 21:36
  • 1
    @thgro Were you saying from analyzer import *? Because that would import everything. If you just want a single function from the module, import only that, e.g. from analyzer import the_function. Have as few global variables as possible (which is probably zero: constants are okay, but better to make them Enums). If a function needs a variable, pass it in as a parameter. If you keep passing the same parameters, group them into a class or a namedtuple. Commented May 17, 2020 at 9:27
  • 1
    Regarding your attempt to use subprocess: You can't just convert a list of lists into a string and then expect python to convert it back. You're doing things the hard way, but if you insist, look at ast.literal_eval Commented May 17, 2020 at 9:30

1 Answer 1

11

1- exec command:

python2:

execfile('test.py') 

python3:

exec(open('test.py').read()) 

2- os command:

test1.py:

import os #os.system('python test2.py') os.system("python test2.py arg1 arg2") 

test2.py:

import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv) 

3- subprocess command:

from subprocess import call call(["python", "test.py"]) 

for passing argument and shell command use subprocess(plz see this Link):

import subprocess # Simple command subprocess.call(['ls', '-1'], shell=True) 

another example code:

file1.py:

args ='python file2.py id ' + 1 subprocess.call(args) 

file2.py:

import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv) 

4- socket pogramming: share data between two or many python file you can use socket programming: see this Link.

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

2 Comments

Thanks taher. I added some code to my question that attempts to use subprocess. Can you show me what I’m doing wrong with that code?
import sys in analyzer.py and remove shell=True in subprocess.call(my_lst_str,shell=True) => subprocess.call(my_lst_str) and then test your code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.