How do I execute another PY program in a PY program?

Source: Internet
Author: User
Tags perl interpreter perl script
Searched on the search engine, no results
A detailed description of the problem: Assume that there is a "a.py" on the desktop, when a.py is run it will automatically open "b.py" (not on the desktop, but on the other path, and this path is known).

The implementation effect is similar to the following code:

Import OS Start_dire = r "E:\*.txt" OS . system (%s"%start_dire)

Reply content:

Considering that the master is a novice, I'll say a few more ways.

0. Runpy.run_path (' my_awesome_program.py ')
This method is preferred, the whole platform is universal


1. Import the file directly (if the file does not add __name__== ' __main__ '), if the filename is a variable, use __import__ (' My_awesome_program ')

2. Os.startfile (' my_awesome_program.py ')
this is windows-only., you can use the same line, a little larger items will not be used

3. Os.system (' python '%s '% ' my_awesome_program.py ')
But if the other computer's python does not hang in the%path%, it is still for your own use only

4. Use subprocess, the command line ibid, use Baidu or check the documents yourself
The same is for your own use only

5. You try execfile (' my_awesome_program.py '), I haven't tried, I'm not sure.
UPDATE: I tried, can be, as if this is a platform-wide universal. Python does not know, but the Perl language has a built-in variable, $^x, whose content is the path of the Perl interpreter that is executing the current script. You can use the contents of that variable to execute another Perl script:
system($^X, "another_script.pl");
Read the. py file first, and then use the Eval function to execute the. py file content, or at least two methods with import, assuming that the b.py is in the root directory of the E-drive:
1. Add the path to the system path where the b.py is located:
importsyssys.path.append('E:/')importbb.foo()
Change Explorer.exe to Python.exe, try it.
You can also use import to execute code in other files.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.