Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

python - IPython workflow (edit, run)

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Two good resources:


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~1empipython_edit_ar8veu.py

In the file I put:

x = "Hello World"
print 3

After saving and quitting the file:

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...