Emacs python

来自linux中国网wiki
Evan讨论 | 贡献2019年10月14日 (一) 13:48的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

good Jedi.el - Python auto-completion for Emacs

安装el-get插件管理器

手动把插件下载下来安装插件的过程费时费力,所以使用el-get来管理和安装插件,就像使用Debian/Ubuntu时使用的apt一样,安装软件省时省力。

 编辑~/.emacs文件,加入如下内容:

(add-to-list 'load-path "~/.emacs.d/el-get/el-get")

(unless (require 'el-get nil 'noerror)
  (with-current-buffer
      (url-retrieve-synchronously
       "https://raw.github.com/dimitri/el-get/master/el-get-install.el")
    (goto-char (point-max))
    (eval-print-last-sexp)))

(el-get 'sync)


(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:complete-on-dot t)
;;    保存,随后启动Emacs,它会自动安装el-get;这个过程可能会比较长,不过我在公司试了 不算慢 毕竟 上了github.com的

trouble

emacs --debug-init

Debugger entered--Lisp error: (error "Could not create connection to raw.github.com:443")


解决办法  在另外一台可以的机器全同步就行啦 
rsync  -avz .emacs [email protected]:/home/evan/
rsync  -avz .emacs.d/  [email protected]:/home/evan/.emacs.d/

安装jedi

按照官网说法,jedi是一个用于IDE和调试器的Python静态分析工具;之前我们安装了el-get,因此安装jedi就轻松许多,在Emacs下键入(其中『RET』表示回车):
也就是要回车两次   第一次回车后出现 Install package:  输入 软件名 jedi 再回车

M-x el-get-install RET jedi RET


如果el-get 不然 就用 
M-x package-install RET jedi RET or


如果没有安装virtualenv,请在终端中使用pip安装:

 pip install virtualenv

最后在Emacs中键入:
 M-x jedi:install-server

基本配置加强

; place on   ~/.emacs
(defun my-python-mode-config ()
  (setq python-indent-offset 4
        python-indent 4
        indent-tabs-mode nil
        default-tab-width 4

        ;; 设置 run-python 的参数
        python-shell-interpreter "ipython"
        python-shell-interpreter-args "-i"
        python-shell-prompt-regexp "In \\[[0-9]+\\]: "
        python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
        python-shell-completion-setup-code "from IPython.core.completerlib import module_completion"
        python-shell-completion-module-string-code "';'.join(module_completion('''%s'''))\n"
        python-shell-completion-string-code "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

  (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
  (hs-minor-mode t)                     ;开启 hs-minor-mode 以支持代码折叠
  (auto-fill-mode 0)                    ;关闭 auto-fill-mode,拒绝自动折行
  ;(whitespace-mode t)                   ;开启 whitespace-mode 对制表符和行为空格高亮
  ;(hl-line-mode t)                      ;开启 hl-line-mode 对当前行进行高亮
  (pretty-symbols-mode t)               ;开启 pretty-symbols-mode 将 lambda 显示成希腊字符 λ
  (set (make-local-variable 'electric-indent-mode) nil)) ; 建议不要关闭,  关闭自动缩进
  )


(add-hook 'python-mode-hook 'my-python-mode-config)
;jq

trouble

Can’t guess python-indent-offset, using defaults: 4

https://segmentfault.com/q/1010000000266278

https://www.cnblogs.com/wangxiaole/articles/7643029.html

在家里如下 这可以用package普通方法安装

Warning (initialization): An error occurred while loading ‘/home/evan/.emacs’:

error: Could not create connection to raw.github.com:443 看来要ss

see also

为Emacs配置Python开发环境

Jedi.el - Python auto-completion for Emacs

加强 Emacs 的 Python3 开发环境配置

00用这个

Usage
回车是 自动补   ctrl+n 才是另起一个新行 没格式关联的
#notice 这个有个问题  第一个func 和第二个func时 会多出4个空格 为什么 呢 


https://github.com/gabrielelanaro/emacs-for-python

最省力的安装是,直接安装别人整合好的一个配置emacs-for-python 
cd  ~/.emacs.d/
git  clone https://github.com/gabrielelanaro/emacs-for-python.git

直接下载下来,并在.emacs文件中添加: 

(load-file "~/.emacs.d/emacs-for-python/epy-init.el")  

;;配置代码错误检测,添加: 
(epy-setup-checker "pyflakes %f")  


vim  ~/.emacs.d/emacs-for-python/epy-init.el

;; 添加显示行号 试过 这个一定有效果
(global-linum-mode 1)




哈哈
不过我觉得还是自己一个个安装比较好,不然哪天来个emacs-for-clojure,后天在来个emacs-for-ruby之类的难保里面的package没有重复冲突的

11

touch .emacs.d/init.el


cat .emacs.d/init.el

;; init.el --- Emacs configuration

;; Set OSX function key as Meta


;; INSTALL PACKAGES
;; --------------------------------------

(require 'package)
(add-to-list 'package-archives                                                                                                        
             '("elpy" . "http://jorgenschaefer.github.io/packages/"))   
(add-to-list 'package-archives
       '("melpa" . "https://melpa.org/packages/") t)

;; activate all packages
(package-initialize)

;; fetch the list of packages available 
(unless package-archive-contents
  (package-refresh-contents))

;; define list of packages to install
(defvar myPackages
  '(better-defaults
    material-theme
    exec-path-from-shell
    elpy
    pyenv-mode))

;; install all packages in list
(mapc #'(lambda (package)
    (unless (package-installed-p package)
      (package-install package)))
      myPackages)

;; BASIC CUSTOMIZATION
;; --------------------------------------

(setq inhibit-startup-message t)   ;; hide the startup message
(load-theme 'material t)           ;; load material theme
(global-linum-mode t)              ;; enable line numbers globally
(setq linum-format "%4d \u2502 ")  ;; format line number spacing
;; Allow hash to be entered
(global-set-key (kbd "M-3") '(lambda () (interactive) (insert "#")))

;; Use shell's $PATH
(exec-path-from-shell-copy-env "PATH")

;; define list of packages to install
(defvar myPackages
  '(better-defaults
    material-theme
    elpy))

(elpy-enable)
(pyenv-mode)
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")


;; init.el ends here
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages
   (quote
    (pyenv-mode elpy exec-path-from-shell material-theme better-defaults))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )


see also

要看的py

Emacs中打造强大的Python IDE

Emacs:最好的Python编辑器

Emacs – The Best Python Editor

http://matrixlyb.is-programmer.com/posts/100431.html

https://github.com/redguardtoo/emacs.d

https://www.cnblogs.com/kunyuanjushi/p/5951405.html


linuxchina package

Turning emacs into a Python IDE

PythonProgrammingInEmacs

Emacs配置Python IDE


赋闲在家的TO DO LIST Emacs入门指南及配置Python开发IDE