IPython 工作流(编辑、运行) [英] IPython workflow (edit, run)

查看:37
本文介绍了IPython 工作流(编辑、运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于 IPython 的 GUI 允许我打开/运行/编辑 Python 文件?我在 IDLE 中的工作方式是打开两个窗口:shell 和一个 .py 文件.我编辑 .py 文件,运行它,并与 shell 中的结果进行交互.

Is there a GUI for IPython that allows me to open/run/edit Python files? My way of working in IDLE is to have two windows open: the shell and a .py file. I edit the .py file, run it, and interact with the results in the shell.

是否可以像这样使用 IPython?或者有其他的工作方式吗?

Is it possible to use IPython like this? Or is there an alternative way of working?

推荐答案

当我使用 Python 时,我通常会打开两个终端窗口 - 一个使用 IPython,另一个使用相当定制的 Vim.

When I'm working with python, I usually have two terminal windows open - one with IPython, and the other with a fairly customized Vim.

两个不错的资源:

虽然听起来你想要的是IPython的魔法函数%ed/%edit:

Though it sounds like what you want is IPython's magic function %ed/%edit:

您可以执行的操作的示例:

An example of what you can do:

In [72]: %ed
IPython will make a temporary file named: c:docume~1wjwe312locals~1	empipython_edit_ar8veu.py

在我放的文件中:

x = "Hello World"
print 3

保存并退出文件后:

Editing... done. Executing edited code...
3
Out[72]: "x = 'Hello world'
print 3
"

In [73]: x
Out[73]: 'Hello world'

您可以定义函数或其他任何东西 - 请记住,文件的内容将在您关闭时执行.

You can define functions or anything else - just remember that the contents of the file will be executed when you close it.

另一个类似的工作流程是cd 到包含您正在使用您喜欢的编辑器编辑的 Python 脚本的目录.然后您可以从 IPython 中%run 脚本,您将可以访问文件中定义的所有内容.例如,如果您在 /home/myself 目录中的文件 test.py 中有以下内容:

Another similar workflow is to cd to the directory containing your Python script that you're editing with your favorite editor. Then you can %run the script from within IPython and you'll have access to everything defined in the file. For instance, if you have the following in the file test.py in your /home/myself directory:

    class Tester(object):
        def __init__(self):
            print "hi"

    def knightme(name):
        print "Hello, Sir ", name

然后您可以执行以下操作:

Then you can do the following:

In [42]: cd /home/myself
/home/myself

In [43]: %run test.py # <Tab> autocomplete also works

In [44]: knightme('John')
Hello, Sir  John

In [45]: t = Tester()
Hi

这些工作流程的混合或其中之一应该会为您提供与您习惯于在 IDLE 中工作的方式非常相似的东西.

Either a mix or one of those workflows should give you something very similar to the way you're used to working in IDLE.

这篇关于IPython 工作流(编辑、运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆