简体   繁体   中英

How to call another python file, run it, and replace main.py with the new .py file?

There are answers to questions like:

How to run a py file from another py file?

But no answers have told how to replace main.py with the recently-ran file, but still jeep main.py. I'm using python 3, and my execute command is this:

exec(open('newfile.py').read())

How can I replace main.py with newfile.py, but keep main.py?

Sorry. I'm not clear for your Question but I just try to created first:py like this:

first.py

print('Hello world')

and then make second.py like this:

second.py

import os
os.system('python first.py')

and when you want to run on CLI you can type this command在此处输入图像描述 this my environment in today's folder: today folder: 在此处输入图像描述

If not your work or clear for your Question feel free to asking more.

I just try to created first.py like this:

print('hello world!')

And second.py like this:

import first

Then first.py will run in the Python environment that second.py run. But in first.py __name__ value will be 'first' and not '__main__'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM