GEdit/Python execution plugin?
Asked Answered
C

8

7

I'm just starting out learning python with GEdit plus various plugins as my IDE.

Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keypress, gets executed in the F# console.

Is there a similar facility/plugin which would enable this sort of behaviour for GEdit/Python? I do have various execution type plugins (Run In Python,Better Python Console) but they don't give me this particular behaviour - or at least I'm not sure how to configure them to give me this. I find it useful because in learning python, I have some test code I want to execute particular individual lines or small segments of code (rather then a complete file) to try and understand what they are doing (and the copy/paste can get a bit tiresome)

... or perhaps there is a better way to do code exploration?

Many thx

Simon

Chilon answered 8/6, 2010 at 5:51 Comment(0)
B
1

To answer your second question, and hopefully guide you in a direction you'll be happier with, I think you ought to consider trying some different editors. There are many with more powerful code exploration features than GEdit has. Check out this post:

What IDE to use for Python?

Blacksmith answered 8/6, 2010 at 6:4 Comment(0)
F
18

Yes, you use "external tools plugin"

As an example,

  1. Edit > Preferences
  2. Plugins
  3. Tick "External Tools"
  4. Close the Preferences Window

  5. Tools > Manage External Tools

  6. Click the "Add new too" icon in the bottom left
  7. Name it "Execute Highlighted Python Code"
  8. give it a keyboard shortcut
  9. change the input combo box to : "highlighted selection"
  10. change the output to : "Display in Bottom Pane"
  11. In the editor window for the tool, replace everything with :

.

#!/usr/bin/env python
import sys
result = eval(sys.stdin.read())
print expression, "=>", result, type(result)

.

Fairman answered 22/9, 2010 at 3:37 Comment(3)
FWIW, in gedit 3.8.3 I get a syntax error even when copying the same syntax into ipython works fine.Elephantine
Excellent idea. I, however, would use exec in line 2, and have the print statement in line 3 removed, since that would help in writing code in a more portable fashionWhimsicality
Doesn't work. Besides using the obsolete python2 syntax for printing, it also complains about expression variable not existing. This answer works.Boris
B
5

If you wish to see the result of entire .py file, you can put this code in your new created external tool window

#!/usr/bin/env python
import sys
exec(sys.stdin.read())

and change the Input to Current document.

Bales answered 1/8, 2012 at 21:53 Comment(0)
S
3

For python, You can use "external tools plugin":

#!/bin/sh
python3 "$GEDIT_CURRENT_DOCUMENT_PATH"

Option of external tool: Save: Current Document Input: Current Document Output: Display in bottom panel

Language: Python or Python3

Don't forget the quotes around $GEDIT_CURRENT_DOCUMENT_PATH....

Sterne answered 3/12, 2016 at 10:3 Comment(2)
Yes, this is one simple solution. I did this too.Stefaniestefano
Works as of 2020. FTR, I had some problem with output not appearing, which turned out that some shortcuts were assigned but haven't been triggering the action. In fact, most of the shortcuts has this problem. To figure out which one works I had to leave a syntax error inside the plugin text field, and keep reassigning the keybinding till I saw gedit complains about a syntax error.Boris
B
1

To answer your second question, and hopefully guide you in a direction you'll be happier with, I think you ought to consider trying some different editors. There are many with more powerful code exploration features than GEdit has. Check out this post:

What IDE to use for Python?

Blacksmith answered 8/6, 2010 at 6:4 Comment(0)
E
1

I installed iPython console in gedit and do most of my simple scripting in it, but gedit is a very simple editor, so it'll not have some advance feature like an IDE

But if you want code exploring, or auto completion, I recommend a real IDE like Eclipse.

If you just want a editor, KomodoEdit is fine.

Eliza answered 8/6, 2010 at 7:1 Comment(0)
B
1

What I do is keep a file called python_temp.py. I have a shortcut to it in my dock. I use it as a scratch pad. Whenever I want to quickly run some code, I copy the code, click the shortcut in the doc, paste in the text and hit f5 to run. Quick, easy, simple, flexible.

Bagger answered 8/6, 2010 at 7:11 Comment(0)
Z
1

I think what you're looking for is http://live.gnome.org/Gedit/Plugins/BetterPythonConsole.

You hit F5 and it runs the code in your file in a IDLE-like console. I don't know if it can only run selected code. (I don't think it can) but you can always copy the needed code in a new window and run it from there.

Have a look through the plugin list for other interesting stuff: http://live.gnome.org/Gedit/Plugins

Zermatt answered 1/8, 2010 at 18:59 Comment(0)
S
1

The closest to a decent IDE... Install gedit-developer-plugins (through synaptic || apt-get) and don't forget to enable (what you need) from gEdit's plugins (Edit->Preferences [tab] plugins) and happy coding

Sliest answered 20/9, 2013 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.