External editor for IPython notebook

2019-02-02 02:08发布

问题:

I am using IPython notebook and I want to edit programs in an external editor. How do I get the %edit file_name.py to open an editor such as Notepad++.

回答1:

Running %edit? will give you the help for the %edit magic function.

You need to set c.TerminalInteractiveShell.editor, which is in your ipython_config.py. I'm not quite sure where this is located in Windows; on OS X and Linux, it is in ~/.ipython. You'll want to set the variable to be the full path of the editor you want.

Alternatively, you can create an environment variable EDITOR in Windows itself, and set that equal to the full path of the editor you want. iPython should use that.



回答2:

I'm using Windows 7 and 8 (and 10TP) and Python 3.4.2.

I started with ipython locate to tell me where ipython thought config files suggested elsewhere should be. When I saw it was different I read around and came up with the following:

  • On my system, the ipython locate gave me c:\users\osmith\.ipython, not the _ipython you'll see mentioned in the YouTube videos done with Windows XP,
  • Look in the directory ipython locate specifies for a profile directory; if you aren't actively doing anything with ipython profiles, it should be .ipython\profile_default, if you are using profiles, then I leave it to you to s/profile_default/${YOUR_PROFILE_NAME}/g
  • Check the profile_default directory for a ipython_config.py file, if it's not there, tell IPython to initialize itself: ipython profile create
  • Open the config file in a text editor,

If you are the kind of person who hasn't messed around with their console overly much and installs things in standard places, you can skip straight to this step by typing: ipython profile create followed by start notepad .ipython\profile_default\ipython_config.py.

  • Search for the string c.TerminalInteractiveShell.editor,
  • The comment above this indicates you can also use the EDITOR environment variable, but hard coding file paths never hurt anyone so lets do eet:
  • Copy the line and remove the leading hash and spaces from the copy.
  • Replace the text between the apostrophes ('notepad') with the path of our desired editor, e.g.

    c.TerminalInteractiveShell.editor = 'c:/program files (x86)/noddyeditor/noddy.exe'

There is a catch here, though; some modern editors get a bit fancy and automatically and, when invoked like this, detach from the console. Notepad++ and Sublime Text, for example. Sublime accepts a "--wait" option, which works some of the time; this tells the command invocation to hang around until you close the file, for some definition of until and some other definition of close.

However, the following setting will work most of the time for sublime text:

c.TerminalInteractiveShell.editor = '"c:/program files/sublime text 3/subl.exe" --wait'

(assuming c:\program files\ is where your sublime text 3 directory is)



回答3:

Try the 'Pycharm' editor
This works for me.