Python run shortcut in GEdit
Asked Answered
G

2

8

I'd like a shortcut key for GEdit that will run the currently open .py file when I press, say, F5. I have a script that does this via an external Terminal window, but I'm having more trouble creating a version that uses the internal output window (Shell Output, I guess), since I can't find a good way to grab the pyenv details from the ~./bashrc file. Working with pyenv is mandatory.

Here's what I have via GEdit's External Tools plugin:


UNSOLVED: Internal Shell Output method:

I wanted to get access to the pyenv settings in ~./bashrc, so I tried this External Tools script:

#!/bin/bash
set +m
bash -i python $GEDIT_DOCUMENTS_PATH

This works (thanks to -i), but it gives me the "bash: no job control in this shell" warning. Running set +m should get rid of this message, but it doesn't.

So I moved the relevant stuff I had at the end of ~/.bashrc over to this script, which is not ideal at all:

#!/bin/bash

export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
  export PATH="${PYENV_ROOT}/bin:${PATH}"
  eval "$(pyenv init -)"
fi

export PYENV_VERSION=3.3.4
export LD_LIBRARY_PATH=~/.pyenv/versions/3.3.4/lib/python3.3/site-packages/PySide-1.2.1-py3.3.egg/PySide/
python $GEDIT_CURRENT_DOCUMENT_NAME

Problems: This last block is terrible. It's just copied from ~/.bashrc, and it even has to include PySide data that ~/.bashrc should take care of. Also, for some reason, using this method always outputs the first line of the .py file (say, import sys). Obviously, no input() can be given using this method, and outputting to GEdit's Embedded Terminal seems impossible. Also, I can't get rid of the "Done" message, even by using set +m or running the command in a subshell.


SOLVED: External Terminal window method:

#!/bin/sh
gnome-terminal -x $SHELL -ic "python $GEDIT_CURRENT_DOCUMENT_NAME; printf \"\nPress any key to continue.\"; read -n 1 -s"

or, define a Terminal profile called, say, Wait, that sets Title and Command->When terminal exits: Hold the terminal open, and do this:

#!/bin/sh
gnome-terminal --profile=Wait -x $SHELL -ic "python $GEDIT_CURRENT_DOCUMENT_NAME; printf \"\nPress any key to continue.\""

This gives a "status 0" message though, so the other method is better. Both methods use an interactive shell to access ~/.bashrc.

Georgianngeorgianna answered 4/3, 2014 at 5:53 Comment(1)
Have you considered using Sublime Text? While not F/OSS, it has several ways of running Python files, including the ability to generate multiple "build systems" for different versions/virtualenvs, and the SublimeREPL plugin lets you run one or more Python interpreters within the editor itself.Blindheim
N
6

Steps to add your custom shortcut key and functionality in GEdit:

1) Open the Manage External Tools.

2) Add a tool

3) Give the tool a name.

4) Enter this code:

#!/bin/sh
python $GEDIT_DOCUMENTS_PATH

5) Give Shortcut Key as F5 by typing directly the function key F5 into the box.

To run the current file, you should save it first. You can now see the output in Shell Output window that comes up when you run the command either through F5 or manually clicking on the command.

Of course, you can modify it to suit your own needs.

Nitriding answered 4/3, 2014 at 6:17 Comment(1)
Thanks for posting this. I ended up having a lot more trouble than expected making this work with pyenv, so I've edited the original question to include more detail about the current status of the script.Sosthena
A
1

I wanted the same. After reading your post the answers and comments, I tried myself around.

To run only the currently open document, open (I am using gedit under Ubuntu 14.04.4 LTS) under Tools>Manage External Tools pressing '+' to add a new tool: and type in into the screen for the shell script:

#!/bin/sh
# run the current document in python
python $GEDIT_CURRENT_DOCUMENT_PATH

The $GEDIT_DOCUMENTS_PATH would apply it to all folders in your current folder of your document - but you wanted to run only the current document isn't it? And the other suggestions: I don't understand why one should make it unnecessarily complicated and why one should ask again - because the moment you press the key you want that thing to be executed, isn't it?

I tried it myself and it works flawlessly.

Adamek answered 4/3, 2014 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.