How to Run a Python File Using Another Python File Using Subprocess (Very Easy)
Вставка
- Опубліковано 19 чер 2022
- It's very simple. In this video, I demonstrate how to run a python file using another python file. This is a nice trick to know and can be useful in some cases. Simply, import the call function from subprocess and use the call function.
I hope you guys find this video helpful. Feel free to like, subscribe, and share this video.
Code used in this video:
from subprocess import call
def open_py_file():
call(["python", "app.py"])
open_py_file() - Розваги
Mad lad. Took me decades too long to find an explanation for this
Cheers!
I like how this is short and very accurate, NC
Thanks man. That's exactly what i looked for!
Glad I could help!
Very nice ! Short and good. Many thanks. Needed this. 🙂
Ayyy let's go! No worries:)
Thankk youuu mannn!!! That's what i was looking forrr. Gladd i watched ur video.. Thnxuu🤞
I'm so happy you found this video useful:)
Thank you
Excellent to learn about that. This is a good program language. Thank you for sharing 👍
Thank you:)
Thanks bro for helping me!!
No problem:) I'm happy this video helped!
I was looking to open a DIFFERENT command prompt for my py files though, could you help me about that?
does each script utilize their own processes or is it a single one shared to all?
u seem to cover epico topics can u make a tutorial/build a function to be called only x amount of times like for example:-
for i in range(10):
print("hello world") # control string
async_loop(1, lambda: print("world hello")
how do I return data or information from the file that was called? Also can I send input parameters from the main file
Dude. I've been struggling to form any real understanding of code & for some reason this video made a tiny bit of sense to other past videos I watched. Still not much of an understanding, but i seent it now🧠🤕
Yeah glad it was helpful :)
well done!
Thank you:)
is there a way to do this without pausing the program
i dont know but for some reason im giving the command and trying to run it but its returning to the next line
I was going to use this before Chat GPT told me about Queueing.
Oh sick! Thanks for sharing
will ti be possible run this in blender
what is the name of the song at the beginning of the video?
"python: can't open file 'C:\\*****\\****\\*******\\*******\\****** ***\\****\\**\\*******\\******\\**** ***\\********': [Errno 2] No such file or directory" I HAVE THIS STUPID PROBLEM LIKE ALWAYSS
Thanks bro
No worries :)
This was helpful but.... Youstarted well showing each stage slowly and carefully but for the last part with gallery you did too much too fast. It became impossible to follow.
tysm but what if its and executable
thanks !
No problem!
I'm so happy you found this video helpful:)
ty mate
No problem:)
when i call another python with print ('hello') works fine but when I use packages like import requests inside a callable program when it runs, the program errors out with no module found requests.
same problem
You're supposed to import the package you want inside the callable program. Like:-
(runprogram.py)
import requests
from subprocess import call
def open_py_file():
call(["python", "app.py"])
open_py_file()
Hope this helps! If not, sorry.
if ur using a virtual environment then that can be an issue, while using an env, the target file which you want to run will be running in your default python. if the packages are not installed there then you will either need to install then or you can goie the python.exe path in ur env.
What if the python file you need to call is in a different dir? And if you need to pass args to it?
I think for another directory I may be two dots instead of one. Like you want into another directory on same level, you provide the path '../other_directory/script.py'
And for every layer of path you go backwards you have to use two dots again: '../../subdirectory/script.py'
Didn't tested it, but this is what I would first try.
But idk how to pass parameters. Maybe there are optional parameters in this array of the call function for parameters of the called script.
Think you have to read the documentation for "call" of the package "subprocess". Maybe I will do it in some time. I will report if I have the time to do so
Whats the disadvantages of this method?
when I run my file "abc.py" this script, I got the error "No module named 'keyboard". Although I can run "abc.py" manually normally.
Make sure the file directory is correct
Here are some resources:
1. pypi.org/project/subprocess.run/
2. stackoverflow.com/questions/26144305/how-to-install-subprocess-module-for-python
3. pythonprogramming.net/python-3-subprocess-tutorial/
Hey bruhh, thanks for this excellent video, it will be so much helpful if you can send the Source Codes of these two 2:52
I'm glad you found this video helpful:)
from subprocess import call
def open_py_file():
call(["python", "app.py"])
open_py_file()
I will be creating a video on this later, so look forward to it:)
it is very easy but... how can i use it if i create app and there is a lot of menu...
Maybe you can create functions within your main .py file
FileNotFoundError: [WinError 2] The system cannot find the file specified
how do i tackle this?
put both .py file in the same folder
Hello brother, the same program when i run it to my pc it's shows can't open no such file in your directory, but file is available exact the same path , i also tried to gave the whole path of the file but same problem happens again...what can I do now ...
It could be because you might need to install subprocess.run. Try running this command on your terminal:
$ pip install subprocess.run
Let me know if the problem occurs again
Here are some resources:
1. pypi.org/project/subprocess.run/
2. stackoverflow.com/questions/26144305/how-to-install-subprocess-module-for-python
3. pythonprogramming.net/python-3-subprocess-tutorial/
How do to the same but opening an excel file?
You can use the following command:
import os
os.system("start EXCEL.EXE file.xlsx")
Please refer to this document: stackoverflow.com/questions/35940748/use-python-to-launch-excel-file
If a file say test.py has an input statement and this file is being from another file say calling.py just as you told in the video, how can I give input through the console using calling.py?
Calling.py will run test.py, which will ask for an input. I think this will work
Is it possible to exectute a script that is located in another folder and it has been built in another enviroment?
Yes, you can. You can import sys and for sys.executable, you can specify the path.
Here is a useful link: www.digitalocean.com/community/tutorials/how-to-use-subprocess-to-run-external-programs-in-python-3
You can also use subprocess.Popen and specify the path using sys.executable
pymotw.com/2/subprocess/
I hope that helps:)
@@JameSparKing Thank you Jame! I will try this
@@orlandob2269 no problem 😊
Well, it doesnt seem to work for me
Make sure you have an output. You can test it by putting a print statement.
Here are some useful links:
1. www.digitalocean.com/community/tutorials/how-to-use-subprocess-to-run-external-programs-in-python-3
2. stackoverflow.com/questions/19257927/subprocess-not-working-in-python
3. queirozf.com/entries/python-3-subprocess-examples
can I have the code?
Code used in this video:
from subprocess import call
def open_py_file():
call(["python", "app.py"])
open_py_file()