简体   繁体   中英

Import arbitrary Python script with main __name__

I have a wrapper script wrapper.py , in which I modify the environment, sys.path and others. One thing that the wrapper script should then do is to execute an arbitrary other Python script, say foo.py , and make __name__ of foo.py be "main" .

Is there a way of doing that?

using python 3.6, I was able to get this to work.

from importlib.machinery import SourceFileLoader
SourceFileLoader("__main__", "foo.py").load_module()

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