开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜