How can I use meta-dot (M-.) in python with emacs?

做~自己de王妃 提交于 2019-12-04 16:35:55

问题


Is there an equivalent of slime for python?

For example, if I position the cursor on foo() and do M-. (jump to definition) I would like to see the source definition of the function foo

This should work regardless of whether foo is in

1) the local project directory

2) in some ~/.virtualenvs/bar/lib/site-packages

3) in some other python-path

4) virtual env is in use (ie, it should look in my current virtualenv)

Does the pymacs/ropemacs combination do any of this?


回答1:


To avoid the -e you can use etags and with a find you recursively add the py file:

find . -type f -name '*.py' | xargs etags



回答2:


Most of the answers mentioned here are outdated. A simple solution is to use elpy for M-. without etags(which requires additional work).

Install elpy as mentioned here.

M-x package-install elpy

and then install python packages

$ sudo pip install rope jedi

Restart emacs , open any python file and run M-.

Elpy is fully documented, you can read about M-. here.




回答3:


M-. normally runs the "find-tag" function. You should create a TAGS file of your python source files. Then you "visit-tags-table" before doing a M-. That way, Emacs will jump to all the definitions of the tag. Type C-u M-. to jump the next definition of your tag. See find-tag documentation for help. Consult Emacs help to know how to create a TAGS file from python source files.

You can for example use Exuberant Ctags for creating the TAGS file.

Go to the root directory of your python files and do this :

ctags -e -R .

A TAGS file is normally created at the root directory of the project.




回答4:


Accepted answer misses an important point, if you execute etags like find . -type f -name '*.py' | xargs etags then the TAGS file would be generated every time for each file.

The correct way to do it is to append data to the existing TAGS file with --append like

rm -f TAGS
find . -type f -name '*.py' -print0 | xargs -0 etags --append

Also if you want to include identifiers from virtual env site packages dir (e.g.: ~/.virtualenvs/bar/lib/site-packages):

SITEPACKAGES=$(cdvirtualenv;pwd)/lib/python3.6/site-packages/
find $SITEPACKAGES -type f -name '*.py' -print0 | xargs -0 etags -a

*adjust python3.6 to your current Python version




回答5:


The following will index your current project

find . -type f -name '*.py' | xargs etags

But if you want to index your imported libs. You first activate your virtualenv. Then use which python to detect where your libs are and then pipe them to etags.

workon my-project # if using virtualenvwrappwer
source bin/activate # if using virtualenv

find ${$(which python)/\/bin\/python/} -type f -name '*.py' | xargs etags



回答6:


Try emacs's anaconda-mode and company-anaconda packages. Update config:

(eval-after-load "company"
 '(add-to-list 'company-backends 'company-anaconda))
(add-hook 'python-mode-hook 'anaconda-mode)
(add-hook 'python-mode-hook 'company-mode)

Switch to virtualenv with pythonic-activate, if you have one.

Now you've got M-. and you can press M-* to go back!



来源:https://stackoverflow.com/questions/3501040/how-can-i-use-meta-dot-m-in-python-with-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!