开发者

How do I execute (not import) a python script from a python prompt?

开发者 https://www.devze.com 2022-12-15 22:03 出处:网络
I need to execute a Py开发者_如何学JAVAthon script from an already started Python session, as if it were launched from the command line. I\'m thinking of similar to doing source in bash or sh.In Pytho

I need to execute a Py开发者_如何学JAVAthon script from an already started Python session, as if it were launched from the command line. I'm thinking of similar to doing source in bash or sh.


In Python 2, the builtin function execfile does this.

execfile(filename)


If you're running ipython (which I highly recommend for interactive python sessions), you can type:

%run filename 

or

%run filename.py

to execute the module (rather than importing it). You'll get file-name completion, which is great for ReallyLongModuleName.py (not that you'd name your modules like that or anything).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号