I want to run a Python script from another Python script. I want to pass variables like I would using the command line. Show For example, I would run my first script that would iterate through a list of values (0,1,2,3) and pass those to the 2nd script I found Stack Overflow question 1186789 which is a similar question, but ars's answer calls a function, where as I want to run the whole script, not just a function, and balpha's answer calls the script but with no arguments. I changed this to something like the below as a test:
But it is not accepting variables properly. When I print out the
I don't really want to change the original script (i.e. script2.py in my example) since I don't own it. I figure there must be a way to do this; I am just confused how you do it.
asked Sep 23, 2010 at 19:31
Gern BlanstonGern Blanston 42.3k19 gold badges49 silver badges64 bronze badges 8 Try using
answered Sep 23, 2010 at 19:33
Greg HewgillGreg Hewgill 908k177 gold badges1131 silver badges1267 bronze badges 16 This is inherently the wrong thing to do. If you are running a Python script from another Python script, you should communicate through Python instead of through the OS:
In an ideal world, you will be able to call a function inside
If necessary, you can hack
I think this is preferable to passing all your data to the OS and back; that's just silly. answered Sep 23, 2010 at 19:43
11 Ideally, the Python script you want to run will be set up with code like this near the end:
In other words, if the module is called from the command line, it parses the command line options and
then calls another function, If you want to call such a script from another Python script, however, you can simply
answered Sep 23, 2010 at 23:19
kindallkindall 172k34 gold badges268 silver badges300 bronze badges 8 I think the good practice may be something like this;
according to documentation The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:
Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process. Read Here answered Oct 29, 2014 at 8:47
MedhatMedhat 1,55415 silver badges31 bronze badges 0
Duncan Jones 64.7k26 gold badges184 silver badges243 bronze badges answered Nov 12, 2013 at 12:06
NikosNikos 3773 silver badges2 bronze badges 3 How do I run a Python file from another Python file with arguments?How to Execute a Python File with Arguments in Python?. Define a Python file script.py that accesses the arguments using the sys. argv variable accessible via the sys module. ... . Fill the variable sys. ... . Load the Python file script.py into a Python string. ... . Pass the Python string into Python's built-in exec() function.. How do I run one Python file from another Python file?Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.
How do you pass an argument in Python exe?In order to pass arguments to your Python script, you will need to import the sys module. Once this module is imported in your code, upon execution sys. argv will exist, containing a list of all of the arguments passed to your script.
How do I call a Python program from another program?Get one python file to run another, using python 2.7.3 and Ubuntu 12.10:. Put this in main.py: #!/usr/bin/python import yoursubfile.. Put this in yoursubfile.py #!/usr/bin/python print("hello"). Run it: python main.py.. It prints: hello.. |