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()
  • Розваги

КОМЕНТАРІ • 93

  • @Trullatz
    @Trullatz 2 роки тому +6

    Mad lad. Took me decades too long to find an explanation for this

  • @arjaytv98
    @arjaytv98 6 місяців тому

    I like how this is short and very accurate, NC

  • @ceo-s
    @ceo-s Рік тому +4

    Thanks man. That's exactly what i looked for!

  • @iiannRB
    @iiannRB Рік тому +2

    Very nice ! Short and good. Many thanks. Needed this. 🙂

  • @harpreetkaur291
    @harpreetkaur291 Рік тому +1

    Thankk youuu mannn!!! That's what i was looking forrr. Gladd i watched ur video.. Thnxuu🤞

  • @Mombierella
    @Mombierella 2 роки тому +1

    Excellent to learn about that. This is a good program language. Thank you for sharing 👍

  • @sparkingharshil
    @sparkingharshil Рік тому +1

    Thanks bro for helping me!!

    • @JameSparKing
      @JameSparKing  Рік тому

      No problem:) I'm happy this video helped!

  • @volden4902
    @volden4902 4 місяці тому +1

    I was looking to open a DIFFERENT command prompt for my py files though, could you help me about that?

  • @frankieiero6859
    @frankieiero6859 4 місяці тому

    does each script utilize their own processes or is it a single one shared to all?

  • @____________________________x
    @____________________________x 9 місяців тому

    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")

  • @hussainbhavnagarwala2596
    @hussainbhavnagarwala2596 4 місяці тому

    how do I return data or information from the file that was called? Also can I send input parameters from the main file

  • @tnastyartdaddy3127
    @tnastyartdaddy3127 7 місяців тому +1

    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🧠🤕

  • @escapefelicity2913
    @escapefelicity2913 Рік тому +1

    well done!

  • @TWLynx
    @TWLynx 4 місяці тому +1

    is there a way to do this without pausing the program

  • @atikeshivakrishna433
    @atikeshivakrishna433 3 місяці тому

    i dont know but for some reason im giving the command and trying to run it but its returning to the next line

  • @JustRollin
    @JustRollin Рік тому +2

    I was going to use this before Chat GPT told me about Queueing.

  • @chari9922
    @chari9922 2 місяці тому

    will ti be possible run this in blender

  • @Dialga9187
    @Dialga9187 5 місяців тому

    what is the name of the song at the beginning of the video?

  • @mariusduminica2010
    @mariusduminica2010 3 місяці тому +2

    "python: can't open file 'C:\\*****\\****\\*******\\*******\\****** ***\\****\\**\\*******\\******\\**** ***\\********': [Errno 2] No such file or directory" I HAVE THIS STUPID PROBLEM LIKE ALWAYSS

  • @utkarshgupta4209
    @utkarshgupta4209 10 місяців тому +1

    Thanks bro

  • @Stafford674
    @Stafford674 Місяць тому

    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.

  • @Gibbits5.7
    @Gibbits5.7 4 місяці тому

    tysm but what if its and executable

  • @RamachandraBharadwaj
    @RamachandraBharadwaj Рік тому +1

    thanks !

  • @setiyadi_ben
    @setiyadi_ben Рік тому

    ty mate

  • @jaiksah
    @jaiksah 11 місяців тому +1

    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.

    • @Tripbisagasa
      @Tripbisagasa 10 місяців тому +1

      same problem

    • @user-qb9ku5ye4v
      @user-qb9ku5ye4v 5 місяців тому

      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.

    • @AbhiigyanSen
      @AbhiigyanSen 4 місяці тому +1

      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.

  • @Exalted_Example
    @Exalted_Example 6 місяців тому

    What if the python file you need to call is in a different dir? And if you need to pass args to it?

    • @cking9145
      @cking9145 4 місяці тому

      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

  • @Chariz69
    @Chariz69 3 місяці тому

    Whats the disadvantages of this method?

  • @giratinum
    @giratinum Рік тому +1

    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.

    • @JameSparKing
      @JameSparKing  Рік тому

      Make sure the file directory is correct

    • @JameSparKing
      @JameSparKing  Рік тому

      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/

  • @amxn007
    @amxn007 Рік тому +2

    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

    • @JameSparKing
      @JameSparKing  Рік тому

      I'm glad you found this video helpful:)

    • @JameSparKing
      @JameSparKing  Рік тому

      from subprocess import call
      def open_py_file():
      call(["python", "app.py"])
      open_py_file()

    • @JameSparKing
      @JameSparKing  Рік тому

      I will be creating a video on this later, so look forward to it:)

  • @tojoeinstein5558
    @tojoeinstein5558 Рік тому +1

    it is very easy but... how can i use it if i create app and there is a lot of menu...

    • @JameSparKing
      @JameSparKing  Рік тому

      Maybe you can create functions within your main .py file

  • @user-md8uv3ms6t
    @user-md8uv3ms6t 7 місяців тому

    FileNotFoundError: [WinError 2] The system cannot find the file specified
    how do i tackle this?

    • @takedown4949
      @takedown4949 5 місяців тому

      put both .py file in the same folder

  • @UnboxingTales69
    @UnboxingTales69 Рік тому

    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 ...

    • @JameSparKing
      @JameSparKing  Рік тому

      It could be because you might need to install subprocess.run. Try running this command on your terminal:
      $ pip install subprocess.run

    • @JameSparKing
      @JameSparKing  Рік тому

      Let me know if the problem occurs again

    • @JameSparKing
      @JameSparKing  Рік тому

      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/

  • @franciscolozano8860
    @franciscolozano8860 Рік тому +1

    How do to the same but opening an excel file?

    • @JameSparKing
      @JameSparKing  Рік тому

      You can use the following command:
      import os
      os.system("start EXCEL.EXE file.xlsx")

    • @JameSparKing
      @JameSparKing  Рік тому

      Please refer to this document: stackoverflow.com/questions/35940748/use-python-to-launch-excel-file

  • @riyajain4882
    @riyajain4882 Рік тому +1

    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?

    • @JameSparKing
      @JameSparKing  Рік тому

      Calling.py will run test.py, which will ask for an input. I think this will work

  • @orlandob2269
    @orlandob2269 Рік тому +1

    Is it possible to exectute a script that is located in another folder and it has been built in another enviroment?

    • @JameSparKing
      @JameSparKing  Рік тому

      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

    • @JameSparKing
      @JameSparKing  Рік тому +1

      You can also use subprocess.Popen and specify the path using sys.executable
      pymotw.com/2/subprocess/

    • @JameSparKing
      @JameSparKing  Рік тому +1

      I hope that helps:)

    • @orlandob2269
      @orlandob2269 Рік тому +1

      @@JameSparKing Thank you Jame! I will try this

    • @JameSparKing
      @JameSparKing  Рік тому

      @@orlandob2269 no problem 😊

  • @glockfraudd
    @glockfraudd Рік тому +1

    Well, it doesnt seem to work for me

    • @JameSparKing
      @JameSparKing  Рік тому

      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

  • @Macude17
    @Macude17 7 місяців тому

    can I have the code?

    • @JameSparKing
      @JameSparKing  6 місяців тому

      Code used in this video:
      from subprocess import call
      def open_py_file():
      call(["python", "app.py"])
      open_py_file()