Is there any way to get a better terminal in emacs? - python

I'm using emacs for python, and I'd like to have a nice useable shell in emacs to run an interpreter alongside my editing.
Is there any better emacs shell package out there? The default shell is awful.

You say "terminal" in the title and "shell" in the question, yet you refer to an interpreter. It's all rather confusing.
If you want a better Python interpreter than the standard (although I'd suggest you explore the features of python-mode first); check out ipython.el which will give you an IPython interface.
If you want a better terminal, try M-x ansi-term, which will give you colors, etc.
If you want a better shell, are you using eshell? You can use your standard shell with M-x shell or M-x ansi-term as above.

That depends on what shell you are using, in GNU Emacs 23 there are at least 3 built in:
shell - ugly, not working tab
eshell - not ugly but tab not working
term - not ugly and seems like ipython works with all goodies in it
So you might want to try the term mode.

Check out Gabriel Elanaro's collection of extensions to emacs for python on github.

In order to avoid future confusion between shell, terms and interpreters in Emacs, it might be worth reading this article first.

Related

How can I run my own shell from elisp?

I written a simple shell in python and compiled it with nuitka.
My shell as some simple commands, such as "say string", "braille string", "stop" etc.
This program uses python accessible_output package to communicate with screen reader in windows.
Ok, this works well froma a normal shell, or executing it from windows.
Now, I would like run this program from within emacs, such as normal shell in emacs.
I tried some functions, "start-process", "shell-command", but I can't write commands.
My program displays a prompt, like python interpreter, where I can put my commands.
Elisp is able to run python shells, mysql shells, but I'm unable to run my own shell.
Help!
Emacs has a number of different ways to interact with external program. From your text, I suspect you need to look at comint in the emacs manual and the elisp reference manual. Comint is the low level general shell in a buffer functionality (it is what shell mode uses).
Reading between the lines of your post, I would also suggest you have a look at emacspeak. and speechd.el, both of which are both packages which add speech to emacs. Speechd.el is bare bones and uses speech-dispatcher while emacspeak is very feature rich. The emacspeak package uses a Tcl script which communicates with hardware or software speech servers. It also has a mac version written in python which communicates with the OSX accessiblity (voiceOver) subsystem. Looking at how these packages work will likely give you good examples on how to make yours do what you want.
Take a look at how it's done in the nodejs-repl https://github.com/abicky/nodejs-repl.el/blob/develop/nodejs-repl.el (see line 308)
In python-mode.el, the part in question reads
(with-current-buffer
(apply #'make-comint-in-buffer executable py-buffer-name executable nil (split-string-and-unquote args))
See docstring of make-comint-in-buffer for details.
What about just launching your script from inside an emacs shell buffer?
M-x shell RET /path/to/my/script RET

Command history in interpreters in emacs

Inside emacs I am running interpreters for several different languages (python, R, lisp, ...). When I run the interpreters through the terminal in most cases I can use the up arrow to see the last command or line of code that I entered. I no longer have this functionality when I am running the interpreters in emacs. How can I achieve this functionality?
How can I access the command history from the interpreter inside emacs?
Can I do this generally for language X?
At the moment I need to use python, so if anyone knows how to do this specifically with the python interpreter in emacs please let me know!
You can use M-p or Ctrl-up to get to the previous command. The complementary keys M-n or Ctrl-down will get you the next command in history.
Check out Emacs' manual page on the shell history ring.
AFAIS the keys are the same as in M-x shell. See menu In/Out for available keys/commands.

python 3 in emacs

I changed two days ago to Emacs 23, which lately gave me a lot of headache, especially, as I have two Python versions installed, the older 2.7 and 3. As I generally want to start the python 3 interpreter, it would be nice if I could tell Emacs in some way to use python 3 instead of 2.7.
Besides, I could not find a module which helps to highlight python3 syntax. I am currently using python-mode.el for highlighting.
Also, if somebody had a good tip for which module would be best to show the pydoc, I would be very thankful.
Thanks in advance!
If you're using python-mode.el, you can specify the binary to be executed as an inferior process by setting the py-python-command variable, i.e.:
(setq py-python-command "python3")
Naturally, you'll need to provide the name of the binary as it exists on your system in place of "python3", if it differs. In python.el, the analogous variable to set is python-python-command.
As far as using pydoc, there are a few possibilities. First, you can simply execute help() inside the Python inferior process. If you do choose that option, you might find it useful to add the following code to your .emacs file:
(setenv "PAGER" "cat")
This is necessary because interactive pagers (e.g., less, more, most, etc.) don't work particularly well inside inferior process buffers. Second, you can install a Texinfo package containing the documentation and use Emacs's info browser (q.v., Python Programming in Emacs). Finally, if you opt to use python.el, it includes an interactive function called python-describe-symbol that can lookup pydoc help on demand (and I suspect python-mode.el should have something similar). If you search around a bit, I'm sure you can find other methods and/or third-party packages as well.
[RET] = enter or return key.
M = Meta or Alt key.
If you feel like letting Emacs do the heavy lifting for you, go through the Emacs settings dialogue to have Emacs set it automatically.
M-x customize-variable [RET] python-shell-interpreter [RET]
A new buffer should appear where you can customize the python interpreter you are using.
The field to the right of Python Shell Interpreter: will be the current interpreter your are using. In my case it was python so I changed it to python3. Then move the curer to select the Apply and Save button above and hit [RET] to make Emacs respect the new setting. Alternatively you can click the button with the mouse.
Next time you open the Emacs python interpreter, it will be using the new python you set from the setting dialogue.
After I did the following thing, I got the behavior that: when editing a Python file in emacs, C-c C-p creates a new buffer with a Python3 interpreter.
Add to your .emacs file the following:
(defcustom python-shell-interpreter "python3"
"Default Python interpreter for shell."
:type 'string
:group 'python)
The reason I tried this, was because I found
(defcustom python-shell-interpreter "python"
"Default Python interpreter for shell."
:type 'string
:group 'python)
in python.el. And I hypothesized (i) that the "python" could be substituted with "python3" and (ii) that this would override the definition in python.el.
It is likely that there are reasons why this is inelegant or heavy-handed or otherwise bad. I am an emacs newb.
start an python-interpreter
M-x python RET
(the default interpreter)
M-x pythonVERSION
where VERSION means any installed version
Inspired by #serv-inc's answer you can also put
(setq python-shell-interpreter "python3")
into your ./.emacs.d/init.el or where you store your configuration.
It's December 2020, I'm using EMACS 25, otherwise unconfigured for python, and I found that:
(setq python-shell-interpreter "python3")
added to my .emacs file did the business and led to a reasonably intuitive way of working with python3. C-c C-c sends my file to the interpreter (or asks me to start a new interpreter, and tells me how).
Of course, now the python3 interpreter runs for any python file, so python 2 programs don't work.
I am imagining a better world where emacs looks at the #!/usr/bin/env python2 line at the top of my file and figures out which interpreter to start by some kind of insane high-tech magic.
Note python-mode.el knows a hierarchy how to detect the version needed
a shebang precedes setting of py-shell-name
while py-execute-THING-PYTHONVERSION
would precede also shebang for the command being
see menu PyExec

How do I run a python interpreter in Emacs?

I just downloaded GNU emacs23.4, and I already have python3.2 installed in Windows7.
I have been using Python IDLE to edit python files.
The problem is that I can edit python files with Emacs but I do not know how to run python interpreter in Emacs. When i click on "switch to interpreter", then it says "Searching for program: no such file or directory, python"
Someone says i need to make some change on .emacs file, but i do not know where to look for.
And I am very unexperienced and just started to learn programming. I am not familiar with commonly used terminologies. I have been searching for solutions but most of the articles i find on the Internet only confuse me.
so the questions are:
how do i run python interpreter in Emacs?
are there different kind of python interpreter? if so, why do they have different interpreters for one language?
Place this in your .emacs file to set the location of your python interpreter:
(setq python-shell-interpreter "path\to\your\python3.2")
Emacs comes with good manuals and an info mode to help read them. To learn more about .emacs you can use:
M-: (info "(Emacs)Init file") RET.
C-c C-z can do this. It is the key-binding for the command python-switch-to-python
In emacs 25.3.1 I use this to open up a python shell:
M-x run-python
After first adding this to my .emacs file:
(setq python-shell-interpreter "/usr/local/bin/python3")
IF you have python installed, try M-x python-shell
(press and hold ALT while pressing x, then type python-shell, then press enter)
There are different language implementations if that is what you are asking (see a list of them here).
In emacs 24.5.1 with spacemacs 105 (develop branch) and the Python layer enabled ("layer" is a spacemacs concept; see their documentation), I find python-shell-switch-to-shell opens an IPython buffer. I tested macropy.console in such a buffer and it works great.
You probably need to have Python in your windows PATH environment variable. Can you start the interpreter just by typing python in the command window?
I don't have anything special in my emacs.el, but the start interpreter command works just fine.
Also, I recommend reading this blog post, as it contains many useful tips and packages worth installing if using Emacs as a Python IDE.
To simply open an interpreter, you can also use M-x python. If that does not work, try M-x python and hit TAB, which will list more options via auto-completion. One of them should work k if you have python installed.
If you are inclined, it may be worthwhile to check out the Emacs python modes as well - http://www.emacswiki.org/emacs/?action=browse;oldid=PythonMode;id=PythonProgrammingInEmacs
In emacs 24.2 there is python-switch-to-python

How can I save / copy classes & functions I've written in the python interpreter?

How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.

Categories

Resources