How Can I Execute an External Python Script with Arguments from Another Script?

DDD
Release: 2024-10-31 20:58:29
Original
766 people have browsed it

How Can I Execute an External Python Script with Arguments from Another Script?

Executing External Python Scripts with Arguments from Within Another Script

The goal of this discussion is to execute a Python script (script2.py) from within another Python script (script1.py) while passing arguments to script2.py as if they were being inputted from the command line.

One method to accomplish this is by using os.system in script1.py:

<code class="python">os.system("script2.py 1")</code>
Copy after login

This command will execute script2.py with the argument "1" as its first argument.

It's important to note that using execfile to achieve this goal is not recommended because it executes the statements of script2.py within the same execution context as script1.py, resulting in no change in sys.argv in script2.py.

The above is the detailed content of How Can I Execute an External Python Script with Arguments from Another Script?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!