How to execute another py program in one py program?

Source: Internet
Author: User
Tags perl interpreter perl script
A detailed description of the problem that has not been found on the search engine: Suppose there is A ". py ", when. when py is run, "B. "py" (not on the desktop, but on another path, and the path is known ). The implementation result is similar to the following code: importosstart_direrE: * .txtos.system(assumer.exe % s % start_dire) searched on the search engine.
A detailed description of the problem: Suppose there is A ". py ", when. when py is run, "B. "py" (not on the desktop, but on another path, and the path is known ).

The implementation result is similar to the following code:

import osstart_dire = r"E:\*.txt"os.system("explorer.exe %s" %start_dire)
Reply content: considering that the subject is a newbie, I will discuss a few more methods.

0. runpy. run_path ('My _ awesome_program.py ')
This method is preferred and is common across the Platform


1. directly import the file (If NO _ name __= = '_ main _' is added to the file). If the file name is a variable, use _ import _ ('My _ awesome_program ')

2. OS. startfile ('My _ awesome_program.py ')
This is windows-onlyIt's okay to use it on your own. A project that is a little bigger will not be used.

3. OS. system ('python "% s" '% 'my _ awesome_program.py ')
However, if the peer computer's python is not in % path % For your own use only

4. Use subprocess. The command line is the same as above. use Baidu or query the document on your own.
Also For your own use only

5. Try execfile ('My _ awesome_program.py ').
UPDATE: I have tried, YesIt seems that this is also universal across the platform. 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 variable content to execute another Perl script:

system($^X, "another_script.pl");
Read the. py file first, then use the eval function to execute the. py file content, or use the import function in at least two ways. Assume that B. py is in the root directory of the E Disk:
1. Add the path of B. py to the system path:

import syssys.path.append('E:/')import bb.foo()
Replace assumer.exewith python.exe.
You can also run the code in other files through import.

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.