How to run python script from python
How to run python script from python
Run one python script from another python script? [duplicate]
I have tried every thing from
I have looked through all the other similar questions on here and read official Python documentation.
I can’t get this
to run the ceatebid.py file reliably. I got it to work with
but what if I also want to call another script?
I want to call different scripts based on how the questions are answered.
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I’m not sure exactly what you’re trying to do, but in general you should be able to do something like this.
The key to using os.system(«python createbid.py») is to pass in a shell command in string format.
So, you need to define some method in you createbid.py (and other scripts):
then in your main script,
Nowadays, the recommended way to launch other processes is to use the subprocess module.
It’s relatively easy to do. Here’s a simple way to apply it to your problem:
Alternatively, you could use exec (statement in Python2, function in Python3).
The merit of this is that exec allows you to define variables before, and use them inside of the script.
So if you were to define some parameters before running the script, you could still call them in your solution:
Still, this approach is more like a fun trick, and depending on the situation, there should be several ways to do better.
What is the best way to call a script from another script? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I have a script named test1.py which is not in a module. It just has code that should execute when the script itself is run. There are no functions, classes, methods, etc. I have another script which runs as a service. I want to call test1.py from the script running as a service.
File test1.py :
File service.py :
I’m aware of one method which is opening the file, reading the contents, and basically evaluating it. I’m assuming there’s a better way of doing this. Or at least I hope so.
16 Answers 16
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The usual way to do this is something like the following.
This is possible in Python 2 using
See the documentation for the handling of namespaces, if important in your case.
In Python 3, this is possible using (thanks to @fantastory)
However, you should consider using a different approach; your idea (from what I can see) doesn’t look very clean.
File test1.py:
File service.py:
The advantage to this method is that you don’t have to edit an existing Python script to put all its code into a subroutine.
Using os you can make calls directly to your terminal. If you want to be even more specific you can concatenate your input string with local variables, ie.
If you want test1.py to remain executable with the same functionality as when it’s called inside service.py, then do something like:
You should not be doing this. Instead, do:
A simple check of sys.modules can be used to invoke the appropriate action. To keep referring to the script name as a string ( ‘test1’ ), use the ‘import()’ builtin.
As it’s already mentioned, runpy is a nice way to run other scripts or modules from current script.
By the way, it’s quite common for a tracer or debugger to do this, and under such circumstances methods like importing the file directly or running the file in a subprocess usually do not work.
It also needs attention to use exec to run the code. You have to provide proper run_globals to avoid import error or some other issues. Refer to runpy._run_code for details.
Why not just import test1? Every python script is a module. A better way would be to have a function e.g. main/run in test1.py, import test1 and run test1.main(). Or you can execute test1.py as a subprocess.
This process is somewhat un-orthodox, but would work across all python versions,
Suppose you want to execute a script named ‘recommend.py’ inside an ‘if’ condition, then use,
The technique is different, but works!
Add this to your python script.
This executes that command as if it were typed into the shell.
This is an example with subprocess library:
Piece of code as example, with traceback module to catch last error text:
An example to do it using subprocess.
from subprocess import run
According to the given example, this is the best way:
But according to the title, using os.startfile(«path») is the best way as its small and it works. This would execute the file specified. My python version is 3.x +.
Not the answer you’re looking for? Browse other questions tagged python or ask your own question.
Linked
Related
Hot Network Questions
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to execute a python script file with an argument from inside another python script file
My problem is that I want to execute a python file with an argument from inside another python file to get the returned values.
I don’t know if I’ve explained it well.
from the shell I execute this:
and this return me a list of cameras.
Thanks in advance for helping a newbie like me!!
Ok, after take a look at your answers, I have to edit my question to make it more concise and because I don’t understand some answers(sorry, like I said I’m a newbie. ):
getMayaCameras.py and doRender.py are both scripts that you can execute directly from the system shell by adding an argument (or flags, in the doRender.py case) and, if it is possible, I want to still having this possibility so I can choose between execute the UI or execute the script directly from the shell.
I’ve made already some modifications for them to work by importing them from renderUI.py but now they don’t work by themselves.
Is possible to have these scripts working by themselves and still having the possibility of calling them from another script? How exactly? This «separating the logic from the command line argument handling» that you told me before sounds good to me but I don’t know how to implement it on my script (I tried but without success).
That’s why I’m posting here the original code for you to see how I made it, feel free both to make critics and/or correct the code to explain me how I should make it for the script to work properly.
Run a python script from another python script and pass variables to it
There are some similar questions that are asked; but, either the answers didn’t cover a specific part that is a key point for me or I just couldn’t understand. So, let me explain my question:
then it will change the format of the date and store it with a variable as shown below
Then, it will create a folder named by the value of the string ‘folderName’.
After this part, I don’t know what to do.
p1 should run p2 and p3, and pass the value of ‘folderName’ to them, then stop; and, p2 and p3 should create files under the folder named by the value of ‘folderName’.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
p3 will be of a similar structure to p2
You can make use of the subprocess module do perform external commands. It is best to start with much simpler commands first to get the gist of it all. Below is a dummy example:
This will grab the output from echo %USERNAME% and store it. It will also run your three scripts but do nothing fancy with them. You can PIPE the output of the scripts as shown in the first example and feed them back in to your next script.
This is NOT the only way to do this (you can import your other scripts). This is nice if you would like to have an external master script to control and manipulate all your child scripts.
If you haven’t checked our argparse yet, you should.
How to Run One Python Script From Another
In this guide, you’ll see how to run one Python script from another Python script.
More specifically, you’ll see the steps to:
But before we begin, here is a simple template that you may use to run one Python script from another (for Python scripts that are stored in the same folder):
Steps to Run One Python Script From Another
Step 1: Place the Python Scripts in the Same Folder
To start, place your Python scripts in the same folder.
For example, let’s suppose that two Python scripts (called python_1 and python_2) are stored in the same folder:
The ultimate goal is to run the python_2 script from the python_1 script.
Step 2: Add the Syntax
Next, add the syntax to each of your scripts.
For instance, let’s add the following syntax in the python_1 script:
Now let’s add the syntax in the python_2 script:
In this case, the expression of ‘hello world’ would be printed when running the second script.
Note that you must first save the syntax that was captured in the python_2 script before calling it from another script.
Step 3: Run One Python Script From Another
Now you’ll need to run the script from the python_1 box in order to call the second script.
Notice that the results of the python_2 script would be displayed first, and only then the results of the python_1 script would be displayed:
Call a Specific Variable from One Python Script to Another
Let’s now see how to call a specific variable (which we will call ‘x’) from the python_2 script into the python_1 script.
In that case, you’ll need to edit the syntax in the python_1 script to the following:
Next, assign a value (e.g., ‘hello world’) to the ‘x’ variable in the python_2 script:
Don’t forget to save the changes in the python_2 script.
Finally, run the syntax from the python_1 script, and the ‘hello world’ expression would be printed:
Interaction of Variables from the Two Scripts
In the final section of this guide, you’ll see how variables from the two scripts may interact.
For example, let’s suppose that the python_1 script has the variable of y = 2, while the python_2 script has the variable of x = 5. The goal is to sum those two variables and display the results.
First, modify the syntax in the python_1 script to the following:
Then, change the syntax in the python_2 script to:
As before, don’t forget to save the changes in the python_2 script.
Finally, run the syntax from the python_1 script, and you’ll get ‘7’ which is indeed the sum of the two variables:
Источники информации:
- http://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script
- http://stackoverflow.com/questions/4230725/how-to-execute-a-python-script-file-with-an-argument-from-inside-another-python
- http://stackoverflow.com/questions/32318909/run-a-python-script-from-another-python-script-and-pass-variables-to-it
- http://datatofish.com/one-python-script-from-another/