简体   繁体   English

加载.py文件并显示IPython交互式外壳

[英]Load the .py file and display IPython interactive shell

In a .py file, I have the following content .py文件中,我具有以下内容

class Point(object):
    def __init__(self, x, y):
        self.x, self.y = x, y

p1 = Point(1, 1)
p2 = Point(1, 1)
set([p1, p2])

Maybe it is silly, but I want to load the content of this file into python interactive shell, ie IPython . 也许这很愚蠢,但是我想将此文件的内容加载到python交互式shell中,即IPython I thought I could just insert the line 我以为我可以插线

from IPython.terminal import interactiveshell
interactiveshell

at the end of the file, but it didn't work. 在文件末尾,但是没有用。 I thought I could use ipython name_of_the_file.py , but it didn't work either. 我以为我可以使用ipython name_of_the_file.py ,但是它也不起作用。 I would like to instantiate maybe p3 without redefine the class in the interactive shell. 我想实例化p3而不重新定义交互式外壳中的类。

Question : How could I load the file and display the IPython interactive shell? 问题:如何加载文件并显示IPython交互式外壳?

UPDATE 更新

An interesting to know is the %load we could use to load the file into the IPython interactive shell. 有趣的是,我们可以使用%load load将文件加载到IPython交互式shell中。

From the command line you could do $ ipython -c yourfile.py -i , where -c is execute given file, and -i is "do not exit, start the repl" (which is the default unless -c is given. 从命令行可以执行$ ipython -c yourfile.py -i ,其中-c是执行给定的文件,而-i是“不退出,请启动repl”(除非指定-c否则这是默认设置)。

Alternatively if ipython is already started you can use In[1]: %run -i myfile.py to run an already existing file in IPython. 另外,如果ipython已经启动,则可以使用In[1]: %run -i myfile.py在IPython中运行一个已经存在的文件。 It is not exactly the same as IPython -c file -i , and if you came across weird effect look at the options of %run with In[1]: %run? 这是不完全一样IPython -c file -i ,如果您在怪异效果的神色在选项%runIn[1]: %run? .

Have also a look at %autoreload as well that might be able to watch your file for changes without restarting the IPython shell. 还可以看看%autoreload ,它可以在不重新启动IPython shell的情况下监视文件的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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