How do I run a python interpreter in Emacs? - python

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

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

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

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

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.

How to fix pdb in Aquamacs on Mac OS X?

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.

How to use Emacs with Python

I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).
Here what I did.
*
Emacs come with python mode but it is confusing there are two types of mode one is python-mode.el and other one is python.el. I use emacs 23 so mine is python.el (I think). Do I need python-mode too? Code completion does not work when I press M-Tab , instead of it window manager works. I tried Esc-Tab but it says "No match" . How can I enable code completion?
After that I installed ropemacs
sudo aptitude install python-ropemacs
Then I created .emacs file at ~/.emacs
and I added followings to .emacs file
(require 'pymacs)
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
Then when I hit M-/ (Alt-/) it doesn't work when I cick from the menu Rope->Code assist it opens a file dialog for choosing root project folder. I choose current folder which has there are some python code. When I try again Code assist from menu it says
"Completion for x: " nothing but empty set. How can make emacs python code completion work?
Then I downloaded anything.el, anything-config, anything-match-plugin to ~/.emacs.d folder Then I added following lines to .emacs file
(require 'anything-config)
(require 'anything-match-plugin)
(global-set-key "\C-ca" 'anything)
(global-set-key "\C-ce" 'anything-for-files)
Guess what it didnt work. I tried "M-x anything" again I get No match.(I guessed may me combination of C-ca (First control-a then e ) might work it says it isnt defined). Could you explain code completion for python with clear explanations (step by step) to someone dummy as me. Thanks.
Edit: I able emacs work with python with the link. Thanks all for answering
I haven't tried anything, and I haven't had much luck with rope (giant source tree causes my emacs to hang upon any file save). Instead, I find the default completion works well enough for my purposes.
The default completion keybinding is M-/. That runs dabbrev-expand which expands the current word to "the most recent, preceding word for which this is a prefix." It's not perfect: It won't parse types, and it won't search imports, but it works in 90% of the cases.
(You'll have to deactivate rope.)
I think you do want the package python-mode installed! The ropemacs variants appears to be for refactoring only, and pymacs is allows Python as an Emacs-extension language -- neither of which is what you need for standard support.
I'm not really sure you had to do anything fancy to get Python development to work. On gNewSense deltah (fork of Ubuntu 8.04) all I did was edit a .py file with the first line being:
#!/usr/bin/python
And then Emacs just figures it out and gives you python mode options. I didn't have to install anything beyond Emacs.
Then again, this may not be helpful as gNewSense pre-installs Emacs by default. I'll have to do it on one of my vanilla Ubuntu installs.
Emacs worked out of the box for me on Ubuntu 9.10.
Did you try C-c TAB (update imports) before trying code completion? I don't think it work unless you do that.

Categories

Resources