omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Run another Python script from current Python code

    Pythonista
    3
    8
    6562
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • victordomingos
      victordomingos last edited by

      Is it possible in Pythonista3/Python3 to call and run an external script? I have to separate Python3 applications in Pythonista. I want to run the first one and have it calling the second one automatically at some point, without merging its functions. I have read somewhere about an execfile() function, but it seems to be missing from current documentation. Any idea?

      1 Reply Last reply Reply Quote 0
      • bennr01
        bennr01 last edited by

        @victordomingos execfile is deprecated in python3. You could use the runpy-module for running another script.

        1 Reply Last reply Reply Quote 1
        • cvp
          cvp last edited by

          I use

          webbrowser.open('pythonista://folder/name.py?action=run
          
          1 Reply Last reply Reply Quote 0
          • victordomingos
            victordomingos last edited by

            @bennr01 said:

            @victordomingos execfile is deprecated in python3. You could use the runpy-module for running another script.

            It seems that this kind of execution need some adaptation in the code. I have my main code inside a if __name__ == "main": clause. That seems to be stopping my second file to run properly. Do I neet to stop using if __name__ == "main":, or is there a better way to do it?

            1 Reply Last reply Reply Quote 0
            • victordomingos
              victordomingos last edited by

              @cvp said:

              I use
              webbrowser.open('pythonista://folder/name.py?action=run

              Very strangely, i seem to have the same issue reported above. But when I add a print() statement in the end of the second script, outside of the if __name__ == "main": clause, it runs the whole code, including what is inside that clause. Why does that happen?

              1 Reply Last reply Reply Quote 0
              • victordomingos
                victordomingos last edited by

                Actually, after a retry, it seems that only the code outside the if __name__ == "main": clause. The rest of code does not output anything.

                1 Reply Last reply Reply Quote 0
                • bennr01
                  bennr01 last edited by bennr01

                  @victordomingos when using runpy.run_path, runpy sets __name__ to <run_path>. You can pass run_name="__main__" to runpy.run_path to change this.
                  EDIT: Also, use if __name__ == "__main__" instead of if __name__ == "main".

                  1 Reply Last reply Reply Quote 1
                  • victordomingos
                    victordomingos last edited by

                    @bennr01 said:

                    @victordomingos when using runpy.run_path, runpy sets name to <run_path>. You can pass run_name="main" to runpy.run_path to change this.
                    EDIT: Also, use if name == "main" instead of if name == "main".

                    That's what I meant, "main" was a typo here. :-)

                    Thanks for thew help!

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors