简体   繁体   中英

Running Python inside Emacs on Windows

I've just installed Python 2.7 on windows along with IPython.

I'm used to running IPython from within Emacs on Linux, eg

M-x shell

Then type ' ipython ' at the prompt.

This works fine under Linux, but under Windows it hangs after printing the IPython banner text, ie it looks like it's working, but then you never get an IPython prompt.

I can load IPython (and Python) under Windows no problem from a standard cmd terminal, just not from within Emacs.

Anyone else experienced or hopefully solved this issue?

I get the same problem when trying to start plain old Python as well.

In Windows it won't work as easily, and it's very annoying. First, make sure you have installed pyreadline.

Next, I have got it working with a bat file that is in my system path containing:

[ipython.bat]
@python.exe -i C:\devel\Python\2.7-bin\Scripts\ipython.py  --pylab %*

Next get python-mode.el and delete ipython.el. The latter is deprecated and python-mode includes its functionality.

Change the path of the ipython.py file accordingly and save it. Now in your Emacs do the following in your init.el file.

(custom-set-variables
 '(py-shell-name "ipython.bat"))

Alternatively, to achieve the last step, do Ch v <RET> py-shell-name and customize it and change it to ipython.bat or the full path of your ipython.bat if the scripts directory is not in your system path. Save for future sessions.

That should get your IPython shell working. There is one more caveat if you want multiple interactive matplotlib figures without hanging your IPython console. The only way I could get around this issue was to use IPython 0.10 instead of the current version.

I wish I had seen this post a while ago. My experiences with running python, ipython from within Emacs are the following:

I tried a number of options (Using python(x,y)-2.7.3.0 on Windows 7)

Using ipython.el:

It still works, provided that you change ipython-command. I do not recommend this, since you loose some functionality from ipython.el by changing ipython-command

You can show figures, but the prompt is frozen until you close the figure

Bare python doesn't work

Changing py-shell-name (as above): My figures are always hanging, when I add the --pylab option

I always get an IPython terminal

Introducing ipython.bat containing

@C:\\Python27\\python.exe -i -u C:\\Python27\\Scripts\\ipython-script.py %*

and changing the path such that ipython.bat i found before ipython.exe

IPython works, but still only one thread (you have to close figure to return to shell)

Python also works and all the remaining functions from python-mode.el

I still have to figure out how to return the shell after opening a figure (within emacs). I got to work using Python 2.6 and and older version of IPython

Using python -i or ipython -i instead of python or ipython worked for me (it gets me the prompt in Emacs, instead of hanging indefinitely).

From python --help :

-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x


However, the resulting python interpreter will probably not be fully functional. You have 2 options :

  • if you are using python from a conda environment : I advise that you switch to a proper Python install. That does not mean deleting Anaconda, it only means to download Python from https://www.python.org/downloads/windows/ and to remove Anaconda's Python from your Windows' %PATH% variable. If you don't, you'll meet many problems with the display of pictures, with the activation of conda (conda activate base), etc etc, and I never succeeded in resolving them all.
  • if you are using Python from python.org/downloads : you're on the right track! Check that Python is accessible from the command line (Win+R cmd python), and if not, add its path to the Windows Path Variable.

Now you need to make the image display work. Out of the box, matplotlib.pyplot lags in Emacs. Here's what you need to do:

First, check that you have downloaded the ipython package ( pip install ipython ) along with matplotlib and numpy (if you use them, but who doesn't : pip install matplotlib , pip install numpy ).

Then find the location of ipython-script.py on your computer (type "ipython-script.py" in the file explorer to find it) and put it as an argument in the variable python-shell-interpreter-args, after the "-i ". Also check that python-shell-interpreter is well set to python . You should have something like that :

(setq python-shell-interpreter "python")
(setq python-shell-interpreter-args "-i c:/Users/YOUR-USER-NAME/AppData/Local/Continuum/anaconda3/Scripts/ipython-script.py") 

That way, you can run a python shell directly from emacs with Mx run-python (or, from a python file, Cc Cp and then Cc Cc).

Of course, you can do the same from a shell terminal too if you prefer, with the command : python -i "c:/Users/YOUR-USER-NAME/AppData/Local/Continuum/anaconda3/Scripts/ipython-script.py"

or you can create a bat file with the same command. But that's not necessary.

(Tested with Python 3.8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM