12

For example, I have two python files, 'test1.py' and 'test2.py'. I want to import test2 into test1, so that when I run test1, it also runs test2.

However, in order to run properly, test2 requires an input argument. Normally when I run test2 from outside of test1, I just type the argument after the file call in the command line. How do I accomplish this when calling test2 from within test1?

Shapi
5,6334 gold badges31 silver badges40 bronze badges
asked Oct 19, 2015 at 16:00
1
  • 1
    Without knowing how test2 is structured, it's very difficult to say. If whoever wrote it factored the argument parsing into a separate function and/or used if __name__ == '__main__' properly, you can likely just bypass it and inject your parameter straight in. If not, you may have to fiddle with sys.argv. Commented Oct 19, 2015 at 16:05

3 Answers 3

5

Depending on the ability of editing test2.py there are two options:

  1. (Possible to edit) Pack test2.py content into class and pass args in init.

in test1.py file:

from test2 import test2class
t2c = test2class(neededArgumetGoHere)
t2c.main()

in test2.py file:

class test2class:
 def __init__(self, neededArgumetGoHere):
 self.myNeededArgument = neededArgumetGoHere
 def main(self):
 # do stuff here
 pass
# to run it from console like a simple script use
if __name__ == "__main__":
 t2c = test2class(neededArgumetGoHere)
 t2c.main()
  1. (Not possible to edit test2.py) Run test2.py as a subprocess. Check subprocess docs for more info how to use it.

test1.py

from subprocess import call
call(['path/to/python','test2.py','neededArgumetGoHere'])
hnvasa
8604 gold badges13 silver badges26 bronze badges
answered Oct 19, 2015 at 16:51
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you can define your own test1 and test2 and that you are OK with using argparse (which is a good idea anyway):

The nice thing with using argparse is that you can let test2 define a whole bunch of default parameter values that test1 doesn't have to worry about. And, in a way, you have a documented interface for test2's calling.

Cribbed off https://docs.python.org/2/howto/argparse.html

test2.py

import argparse
def get_parser():
 "separate out parser definition in its own function"
 parser = argparse.ArgumentParser()
 parser.add_argument("square", help="display a square of a given number")
 return parser
def main(args):
 "define a main as the test1=>test2 entry point"
 print (int(args.square)**2)
if __name__ == '__main__':
 "standard test2 from command line call"
 parser = get_parser()
 args = parser.parse_args()
 main(args)

audrey:explore jluc$ python test2.py 3

9

test1.py

import test2
import sys
#ask test2 for its parser
parser = test2.get_parser()
try:
 #you can use sys.argv here if you want
 square = sys.argv[1]
except IndexError:
 #argparse expects strings, not int
 square = "5"
#parse the args for test2 based on what test1 wants to do
#by default parse_args uses sys.argv, but you can provide a list
#of strings yourself.
args = parser.parse_args([square])
#call test2 with the parsed args
test2.main(args)

audrey:explore jluc$ python test1.py 6

36

audrey:explore jluc$ python test1.py

25
Mr. T
12.5k10 gold badges39 silver badges67 bronze badges
answered Oct 19, 2015 at 20:27

Comments

0

You can use the call or popen method from subprocess module.

from subprocess import call, Popen
Call(file, args)
Popen(file args)
SalGorithm
9018 silver badges29 bronze badges
answered Apr 2, 2018 at 4:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.