Emacs has this very powerful package Elpy which is usually recommended if you want to code in Python.
Docker can containerize your Python interpreter so that you can seemlessly run your applications on any device.
In Emacs with elpy installed, C-c C-c
binding (C is for Control key) runs elpy-shell-send-region-to-buffer
and is very handy for running on the fly python commands for testing your code. It actually use the content of the variable python-shell-interpreter
for the python interpreter.
The default value of this variable is "python"
, which will most likely run your system-wide python interpreter. What we want is to run the following command which will send our python code to a running container.
docker run -it -v /tmp:/tmp -v $(pwd):/code ufoym/deepo python3
See docker command line reference for details about this command.
However just setting python-shell-interpreter
to this will raise an error.
Python shell interpreter 'docker run -it -v /tmp:/tmp -v $(pwd):/code ufoym/deepo python3' cannot be found. Please set the value 'python-shell-interpreter' to a valid binary.
Emacs needs a shell script, so for example, you can set
(setq python-shell-interpreter "~/.docker-python-shell.sh")
Then the file ~/.docker-python-shell.sh
will contain
#!/bin/bash
docker run -it -v /tmp:/tmp -v $(pwd):/code ufoym/deepo python3
Don't forget to make the script executable.
chmod u+x ~/.docker-python-shell.sh
Two remarks on the docker run
command:
-v /tmp:/tmp
can solve some errors caused by Python trying to read a file contained in/tmp
, see https://stackoverflow.com/questions/43194627/how-to-connect-emacs-elpy-in-buffer-python-interpreter-to-docker-container-v $(pwd):/code
mount the directory of your python script to/code
in the docker container, which is handy if you need to access another module or some data