I've just found IPython and I can report that I'm in deep love. And the affection was immediate. I think this affair will turn into something lasting, like the one I have with screen. Ipython and screen happen to be the best of friends too so it's a triangular drama. Purely platonic, mind you.
The reason IPython hits the soft spots with me are very much because I generally like command prompts, and especially *nix-inspired prompts with inspiration from ksh, csh (yes, chs is a monster, but as a prompt it sport lots of really good features), bash and zsh. And IPython does sure feel like home for a *nix prompt rider. Mixing the system shell and python is also a really good idea. Plus, of course, IPython helps a lot when solving the Python Challenge riddles. Invaluable even.
Now, I love Vim too. Since I learnt vi back in the days there's no turning back. And I'm on Mac when I have a choice. Now I'd like to glue together my IPython + MacVim workflow. What I've done so far is that I start Ipython using:
ipython -e "open -a MacVim"
Thus when I edit from IPython it starts MacVim with the file/module loaded. Could look a bit like so:
In [4]: %run foo #This also "imports" foo anew
hello world
In [5]: edit foo
Editing... done. Executing edited code... #This happens immediately
hello world
In [6]: %run foo
hello SO World
OK. I think this can be improved. Maybe there's a way to tie IPython into MacVim too? Please share your experiences. Of course if you use TextMate or some other fav editor I'm interested too. Maybe some of the lessons are general.
I use Linux, but I believe this tip can be used in OS X too. I use GNU Screen to send IPython commands from Vim as recommended by this tip. This is how I do it:
First, you should open a terminal and start a screen session called 'ipython' or whatever you want, and then start IPython:
$ screen -S ipython
$ ipython
Then you should put this in your .vimrc:
autocmd FileType python map F5 :w<CR>:!screen -x ipython -X stuff $'\%run %:p:h\n'<CR><CR>
Then when you hit F5, it will tell Screen to execute the command '%run file' inside the 'ipython' created previously, where file is your current buffer in Vim.
You can tweak this to execute the command you want inside IPython from Vim. For example I use this:
autocmd FileType python map <F5> :w<CR>:!screen -x ipython -X stuff $'\%reset\ny\n\%cd %:p:h\n\%run %:t\n'<CR><CR>
This executes %reset (answering yes to the prompt), then change to the directory where the current buffer in vim is located and then %run the file. This is specially useful if you have the %pdb active in IPython.
Don't forget that you need an active Screen session called 'ipython' with IPython running inside.
If you like Emacs. There is good support for IPython.
This questions is stale now, but just for reference - if you're using IPython 0.11 with ZeroMQ enabled, take a look at vim-ipython (an older version of which shipped with 0.11).
Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions
in Vim, like what you get with: object?<enter> and object.<tab> in IPython.
Take a look at two screencasts of this plugin in action on this post.
In the ipythonrc.ini file in you home directory change the editor to MacVim.
I've had the same issue. After experimenting this is my solution: if you placed MacVim.app in /Applications and mvim is in your path, in ~/.ipython/ipythonrc change the line
editor 0
to
editor mvim --remote-tab-wait-silent
I think you need to keep MacVim open in the background. You could also use the -f flag. Look at this thread from the MacVim google group.
Related
I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.
I'm wanting to open a terminal from a Python script (not one marked as executable, but actually doing python3 myscript.py to run it), have the terminal run commands, and then keep the terminal open and let the user type commands into it.
EDIT (as suggested): I am primarily needing this for Linux (I'm using Xubuntu, Ubuntu and stuff like that). It would be really nice to know Windows 7/8 and Mac methods, too, since I'd like a cross-platform solution in the long-run. Input for any system would be appreciated, however.
Just so people know some useful stuff pertaining to this, here's some code that may be difficult to come up with without some research. This doesn't allow user-input, but it does keep the window open. The code is specifically for Linux:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 \''+myFilePathString+'\'; echo \'(Press any key to exit the terminal emulator.)\'; read -n 1 -s"');
subprocess.call(params);
To open it with the Python interpreter running afterward, which is about as good, if not better than what I'm looking for, try this:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 -i \''+myFilePathString+'\'"');
subprocess.call(params);
I say these examples may take some time to come up with because passing parameters to bash, which is being opened within another command can be problematic without taking a few steps. Plus, you need to know to use to quotes in the right places, or else, for example, if there's a space in your file path, then you'll have problems and might not know why.
EDIT: For clarity (and part of the answer), I found out that there's a standard way to do this in Windows:
cmd /K [whatever your commands are]
So, if you don't know what I mean try that and see what happens. Here's the URL where I found the information: http://ss64.com/nt/cmd.html
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?
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:
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
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~1\wjwe312\locals~1\temp\ipython_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'\nprint 3\n"
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.
Spyder, previously known as SPyderlib / Spyder2
Pretty lightweight, fast and support almost all features you will ever need to work with a python project. It can edit and run .py files in an embedded IPython instance and then interact with them, set breakpoints, etc.
full-size
Try Spyder, I have spent all day trying to find an IDE which has the functionality of ipython and Spyder just kicks it out of the park..
Autocomplete is top notch right from install, no config files and all that crap, and it has an Ipython terminal in the corner for you to instantly run your code.
big thumbs up
Take a look at DreamPie. Might be what you are looking for.
Personally, I like PyScripter. Unfortunately, it only works on Windows, but also runs perfectly in Wine.
The latest version of IdleX supports IPython within IDLE, as well as the %edit magic. You can run your files from the IDLE editor within the IPython shell many ways, either by F5 (run everything), F9 (run a selection), or Ctrl+Enter (run a subcode).
sudo apt-get install ipython
Once you are done with installing ipython.
Start ipython from terminal (just hit ipython in the ternminal)
To run ravi.py file all you need to do is
%run ravi.py
If you like the work-flow under Matlab, then you probably should try the following two:
1, Try the combination of Spyder and Vim.
Edit python files in Vim (Spyder can reload the file automatically)
Run the code in Spyder (in the same interpreter, which is important for me):
Use F9 to run the current file
Ctrl+F9 to run the selected block
2, Use Vim + conque-shell. (on google code)
Open your preferred Python interpreter in Vim,
e.g., just :ConqueTermSplit python.
then visual select some Python code
press F9 to paste and run it in the Python interpreter buffer.
Note: a few more:
:ConqueTermVSplit python,
:ConqueTerm python
:ConqueTermVSplit rlwrap python
If your interpretor misses readline, you can use rlwrap.
You might like PySlices...
It's kind of a shell/editor hybrid that lets you save your session as special (barely) modified python files called .pyslice files.
It's now part of wxPython, so just install that (v2.8.11 or later) and run "python -m wx.py.PySlices" on the command line to launch it.
That said, I still end up using an external editor for scripts (geany).
I want to suggest excellent plugin for vim that makes two-way integration between Vim and IPython: vim-ipython.
From project page on http://github.com/ivanov/vim-ipython:
Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object? and object. in IPython.
This plugin has one big limitation: it doesn't support python 3 (it's planned).
Personally, I use what #Wayne suggested, a combination of vim and ipython...
However, if you'd prefer a different approach, take a look at spyder.
As of the latest version (1.1) ipython should be fully integrated. If you download an earlier version, things will work fine with ipython as an external shell, but you won't get a few of spyder's nifty features (like viewing all of the currently defined variables in the workspace window).
Spyder is definitely a bit heavyweight, but it's an interesting project.
Another (very, very, new) similar project to take a look at is iep. It will (sort-of) work with ipython as shell, and I'd be willing to be bet that nicer ipython integration will be along before too long. At any rate, iep is essentially a more lightweight alternative to spyder.
Both of these are oriented towards scientific computing, and so have nice integration with things like matplotlib (and thus can automatically run gui main loops in a seperate thread). They're not quite like "normal" IDE's but they may fill the niche you're looking for quite nicely.
You can use the autoreload module in IPython to automatically reload code.
Open jupyter qtconsole or jupyter console and type:
%load_ext autoreload
%autoreload 2
from your_work_file import *
Now every time you save your_work_file.py, it will be automatically reloaded.
Hint: if you want this to happen automatically, put the followinglines in your ipython_config.py file:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Try Ptpython. It has much better integration with VIM. You can directly edit in VIM by just pressing V. It also allows browsing your history.. so you can pretty much code in the shell, and incrementally build up your code.
If you are already familiar with ipython, you can check the advantages of ptpython here:
https://www.youtube.com/watch?v=XDgIDslyAFM
I just use the exclamation mark (!) to run vi as a shell command
In [1]: !vi myScript.py
and when done with editing I just quit vi to get back to the Ipython shell.
To run the script one can then use
In [2]: %run myScript.py
as suggested in another answer and not !python ... because the Python version in ipython might be different from the one in the underlying shell.
If you want to dump some code in a file use the magic %%writefile
In [3]:%%writefile myScript.py
...: print("hello")
...:
...:
Be careful because this will overwrite myScript.py. To append use %%writefile -a.
I'm developing a django app using aquamacs as my ide. Pdb isn't working since upgrading to emacs 23.2.1 using python 2.6.1. When I invoke pdb like this:
M-x pdb
Run pdb (like this): pdb ./manage.py runserver
The gud-manage.py frame appears with this message (and nothing more) -
Current directory is /path/to/my/source/
It isn't responsive to keyboard input, though I can right-click and send a quit or kill signal. It seems like emacs isn't capturing the pdb output correctly.
Has anyone seen this and (hopefully) fixed it? I believe it has something to do with the gud-pdb-marker-regexp variable (see point #2 in link).
Related issues
Seems to have been around since 2007
One person a solution for this problem on Windows (adding -u to the python command in the pdb script). I tried it anyway, but this didn't work for me.
Same issue (Current directory is ...) to me with emacs 23.2 (9). As you mentioned, it is caused by a CR/LF ending and can be fixed by setting the gud-pdb-marker-regexp.
I added the CR (\r) to the gud-pdb-marker-regexp. May you want to add the following line to your .emacs file and give it a try.
(setq gud-pdb-marker-regexp "^> \\([-axx-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n\r]*\\)?[\n\r]")
Not sure if that is the case for you, but just to mention it: PDB hangs for me in Emacs when the source code path contains a space - when I move the python file to a directory without a space in the name, it works (on Emacs 23.1.1).
I have been having this same issue. I had it fixed in 23.1 (http://debbugs.gnu.org/db/56/5653.html) but now in 23.2 that fix no longer works, or at least it doesn't appear to for me. I've just submitted a bug to Emacs explaining the problem in detail and hopefully it will get resolved.
A workaround for this is to execute pdb from the emacs shell:
Open the shell: M-x shell
Enter this in the shell: pdb
This will get pdb working properly within the shell.
This may sound strange, but I need a better way to build python scripts than opening a file with nano/vi, change something, quit the editor, and type in python script.py, over and over again.
I need to build the script on a webserver without any gui. Any ideas how can I improve my workflow?
put this line in your .vimrc file:
:map <F2> :w\|!python %<CR>
now hitting <F2> will save and run your python script
You should give the screen utility a look. While it's not an IDE it is some kind of window manager on the terminal -- i.e. you can have multiple windows and switch between them, which makes especially tasks like this much easier.
You can execute shell commands from within vim.
Using emacs with python-mode you can execute the script with C-c C-c
you can try ipython. using its edit command, it will bring up your editor (nano/vim/etc), you write your script, and then on exiting you're returned to the ipython prompt and the script is automatically executed.
When working with Vim on the console, I have found that using "tabs" in Vim, instead of having multiple Vim instances suspended in the background, makes handling multiple files in Vim more efficient. It takes a bit of getting used to, but it works really well.
You could run XVNC over ssh, which is actually passably responsive for doing this sort of thing and gets you a windowing GUI. I've done this quite effectively over really asthmatic Jetstart DSL services in New Zealand (128K up/ 128K down =8^P) and it's certainly responsive enough for gvim and xterm windows. Another option would be screen, which lets you have multiple textual sessions open and switch between them.
There are actually 2 questions. First is polling for a console IDE for python and the second is a better dev/test/deploy workflow.
For while there are many ways you can write python code in the console, I find a combination of screen, vim and python/ipython is the best as they are usually available on most servers. If you are doing long sessions, I find emacs + python-mode typically involves less typing.
For a better workflow, I would suggest setting up a development environment. You can easily setup a Linux VM on your desktop/laptop easily these days - there isn't a excuse not to even if it's for hobby projects. That opens up a much larger selection of IDEs available to you, such as:
GUI versions of VI and friends
Remote file editing with tramp and testing locally with python-mode inside Emacs
http://www.netbeans.org
and of course http://eclipse.org with the PyDev plugin
I would also setup a SCM to keep track of changes so that you do
better QA and use it to deploy tested changes onto the server.
For example I use Mercurial for my pet projects and I simply tag my repo when it's ready and update the production server to the tag when I deploy. On the devbox, I do:
(hack hack hack, test test test)
hg ci -m 'comment'
hg tag
hg push
Then I jump onto the server and do the following when I deploy:
hg update
restart service/webserver as needed
Well, apart from using one of the more capable console editors (Emacs or vi would come to mind), why do you have to edit it on the web server itself? Just edit it remotely if constant FTP/WebDAV transfer would seem to cumbersome.
Emacs has Tramp Mode, gedit on Linux and bbedit on the Mac support remote editing, too. Probably quite a large number of other editors. In that case you would just edit in on a more capable desktop and restart the script from a shell window.
For what it's worth, VIM alone can do the same tasks as previously posted. I have had the same problem with testing Python from the command line.
My solution was to use the screen command. I split screens vertically, I run Python in one instance of a shell, and on the second screen, I usually edit Python code with VIM.
Command to install screen:
sudo apt-get install screen
The screen package has a bit of a learning curve but there isn't any mystery if you can remember the "Ctrl-Alt ?" command that contains all knowledge.
No GUI is required!