diff --git a/contrib/lang/python/README.md b/contrib/lang/python/README.md
index 55c868bf2a30..d3ffbf8cd22c 100644
--- a/contrib/lang/python/README.md
+++ b/contrib/lang/python/README.md
@@ -10,6 +10,7 @@
- [Install](#install)
- [Key Bindings](#key-bindings)
- [Inferior REPL process](#inferior-repl-process)
+ - [Running Python Script in shell](#running-python-script-in-shell)
- [Testing in Python](#testing-in-python)
- [Other Python commands](#other-python-commands)
- [Django](#django)
@@ -67,12 +68,20 @@ Send code to inferior process commands:
CTRL+j | next item in REPL history
CTRL+k | previous item in REPL history
-### Running Python Script in Comint Mode
+### Running Python Script in shell
-To run a Python script like you would in the shell
-press SPC c C RET to start the Python script in
-Comint mode. This is useful when working with multiple Python files
-since the REPL does not reload changes made in other modules.
+To run a Python script like you would in the shell press SPC m c c
+to start the Python script in comint mode. This is useful when working with
+multiple Python files since the REPL does not reload changes made in other
+modules.
+
+ Key Binding | Description
+----------------------|------------------------------------------------------------
+SPC m c c | Execute current file in a comint shell
+SPC m c C | Execute current file in a comint shell and switch to it in `insert state`
+
+**Note** With the universal argument SPC u you can enter a new
+compilation command.
### Testing in Python
diff --git a/contrib/lang/python/extensions.el b/contrib/lang/python/extensions.el
index c9e5ca09021d..f8a5c85a2e3c 100644
--- a/contrib/lang/python/extensions.el
+++ b/contrib/lang/python/extensions.el
@@ -67,25 +67,3 @@
(setq pylookup-dir (concat dir "/pylookup")
pylookup-program (concat pylookup-dir "/pylookup.py")
pylookup-db-file (concat pylookup-dir "/pylookup.db"))))))
-
-(defun python/init-python-compile ()
- "Initialize Compile command for python buffers"
- ;; set compile command to buffer-file-name
- ;; if buffer-file-name exists
- ;; otherwise error occurs on e.g. org export including python src
- (add-hook 'python-mode-hook
- (lambda ()
- (set (make-local-variable 'compile-command)
- (if buffer-file-name
- (format "python %s" (file-name-nondirectory buffer-file-name))))))
-
- (defadvice compile (before ad-compile-smart activate)
- "Advises `compile' so it sets the argument COMINT to t
- in `python-mode' files"
- (when (derived-mode-p major-mode 'python-mode)
- (save-excursion
- (save-match-data
- (goto-char (point-min))
- ;; set COMINT argument to `t'.
- (ad-set-arg 1 t)))))
-)
diff --git a/contrib/lang/python/packages.el b/contrib/lang/python/packages.el
index 55607cce2461..e8ac75cf909b 100644
--- a/contrib/lang/python/packages.el
+++ b/contrib/lang/python/packages.el
@@ -204,7 +204,37 @@ which require an initialization must be listed explicitly in the list.")
(python-shell-switch-to-shell)
(evil-insert-state))
+ ;; reset compile-command (by default it is `make -k')
+ (setq compile-command nil)
+ (defun spacemacs/python-execute-file (arg)
+ "Execute a python script in a shell."
+ (interactive "P")
+ ;; set compile command to buffer-file-name
+ ;; if buffer-file-name exists
+ ;; otherwise error occurs on e.g. org export including python src
+ ;; universal argument put compile buffer in comint mode
+ (setq universal-argument t)
+ (if arg
+ (call-interactively 'compile)
+ (unless compile-command
+ (setq compile-command (format "python %s" (file-name-nondirectory
+ buffer-file-name))))
+ (compile compile-command t)))
+
+ (defun spacemacs/python-execute-file-focus (arg)
+ "Execute a python script in a shell and switch to the shell buffer in
+`insert state'."
+ (interactive "P")
+ ;; set compile command to buffer-file-name
+ ;; if buffer-file-name exists
+ ;; otherwise error occurs on e.g. org export including python src
+ (spacemacs/python-execute-file arg)
+ (switch-to-buffer-other-window "*compilation*")
+ (evil-insert-state))
+
(evil-leader/set-key-for-mode 'python-mode
+ "mcc" 'spacemacs/python-execute-file
+ "mcC" 'spacemacs/python-execute-file-focus
"mdb" 'python-toggle-breakpoint
"msB" 'python-shell-send-buffer-switch
"msb" 'python-shell-send-buffer