jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.9k stars 261 forks source link

elpy-goto-definition doesn't work perfectly #1367

Open lianzisong opened 6 years ago

lianzisong commented 6 years ago

Summary

here is my project struct:

root: |__dir1: | |_file1.py | |_file2.py |_dir2: |_file3.py |_file4.py

fil1 import the file3 or file2. but it failed to goto the definition either in file2 or in file3. it show s the "No Definition Found".

but it works find with common modules like time/Request..

here is my elpy-config:

Elpy Configuration

Virtualenv........: None RPC Python........: 3.6.1 (c:/Anaconda3/pythonw.exe) Interactive Python: python (c:/Anaconda3/python.exe) Emacs.............: 25.3.1 Elpy..............: 1.20.0 Jedi..............: 0.10.2 (0.12.0 available) Rope..............: 0.10.7 Autopep8..........: 1.3.5 Yapf..............: 0.21.0 Syntax checker....: flake8.exe (c:/Anaconda3/Scripts/flake8.exe)

jorgenschaefer commented 6 years ago

Hello and thanks for the report! It's not always possible for Jedi (the library Elpy uses to find definitions) to find things. It's difficult to guess what's going on here without knowing the contents of the files and what your PYTHONPATH contains.

lianzisong commented 6 years ago

here is the git url : https://github.com/waditu/tushare.git

and I use the M-. at the position describe below: image

U can try it and tell me how to resolve it. thanks.

galaunay commented 6 years ago

I cloned your repo to have a try. It works for me: calling elpy-goto-definition at the point you specified brings me to the beginning of the cons.py file.

The first thing you should try is to update jedi (to 0.12). It may magically solve your issue.

lianzisong commented 6 years ago

thanks for the reply. I upgrade the jedi to the 0.12, but the result is the same. I wonder how the jedi find the definition ,cause I just open a file , how the jedi knows the relations between file1 and the file2? the jedi should know the folder struct at least ,right? maybe I miss some operation ?

here is my elpy-config:

Virtualenv........: None RPC Python........: 3.6.1 (c:/Anaconda3/pythonw.exe) Interactive Python: python (c:/Anaconda3/python.exe) Emacs.............: 25.3.1 Elpy..............: 1.20.0 Jedi..............: 0.12.0 Rope..............: 0.10.7 Autopep8..........: 1.3.5 Yapf..............: 0.21.0 Syntax checker....: flake8.exe (c:/Anaconda3/Scripts/flake8.exe)

galaunay commented 6 years ago

When you open trading.py, Elpy tries to detect the root of the project (you can check what the detected project root is with (elpy-project-root)). In your case, the project root should be your tushare folder.

The RPC process responsible for finding function definition is started in this folder. So when you ask jedi for function definition, jedi is started in the project root folder. It means that jedi should have knowledge of your package and of every files in it.

Could you check what is the detected project root (with (elpy-project-root)) ?

lianzisong commented 6 years ago

thanks for the reply. But I don't have the elpy-project-root command . is it excuted through the M-x? the candidate command is only the 'elpy-profile-buffer-or-region' begin with 'elpy-pro'.

Does that means I don't install the elpy module correctly?

galaunay commented 6 years ago

It is not an interactive function, so you have to call it through Eval. Try M-: (instead than M-x), then type (elpy-project-root) and hit return.

lianzisong commented 6 years ago

I set the elpy-project-root to the source code folder. but it failed to find the definition either.

here is my screen shorcut: image

by the way . from tushare.stock import cons as ct. I can use the M-. jump to the 'tushare.stock' ,but it can only jump into the 'stock/init.py' file. the cons is still 'No define found'.

galaunay commented 6 years ago

It seems to be a problem with jedi then...

could you try creating a file test_jedi.py with the following content alongside trading.py:

# -*- coding: utf-8 -*-
#!/usr/env python

import jedi

line = 17
column = 29
filename = "trading.py"
script = jedi.Script(path=filename,
                     line=line,
                     column=column,
                     encoding='utf-8')
loc = script.goto_assignments()[0]
print("file: {}".format(loc.module_path))
print("fullname: {}".format(loc.full_name))
print("position (line, col): {}, {}".format(loc.line, loc.column))

Then launch it, the output may give us an insight on what's wrong.

lianzisong commented 6 years ago

here is my excute result: Jason@Jason-PC MINGW64 /e/Github/tushare/tushare/stock (master) $ python test_jedi.py file: E:\Github\tushare\tushare\stock\cons.py fullname: tushare.stock.cons position (line, col): 1, 0

it seems that the result is right. So I try the elpy-goto-definition again , but it failed me again.

galaunay commented 6 years ago

Well, jedi seems to work as expected. I really don't know what happen here.

You can try this script, that mimic perfectly the jedi implementation in Elpy. The result should look like this:

Location from 'goto_definition':
    [<Definition module cons>]
    /home/glaunay/tmp/tushare/tushare/stock/cons.py
    tushare.stock.cons
    line: 1, col: 0
Returned location: <Definition module cons>
lianzisong commented 6 years ago

thanks for the reply . I execute the script , and it shows: Location from 'goto_definition': [] E:\Github\tushare\tushare\stock\cons.py tushare.stock.cons line: 1, col: 0 Returned location:

BTW, the script seems don't assign the coding-system to the open operation, so ,at first the execute result will report the encoding error. then I modify the script ,add the encoding='utf-8' to the open function. then the error disappear.

My OS is win 10. and default language is chinese. So I wonder , does the character encoding cause the jedi failed to find the definition?

galaunay commented 6 years ago

I don't think it is an encoding problem, because it works for me (on your package). An issue with windows is possible, but I don't have any machine on window with emacs to try.

Are you sure there is nothing printed between the brackets ?

Location from 'goto_definition':
[]    <=== here 
E:\Github\tushare\tushare\stock\cons.py
tushare.stock.cons
line: 1, col: 0
Returned location:
lianzisong commented 6 years ago

NO. I copy the content again,here is the execute result . Location from 'goto_definition': [< Definition module cons >] E:\Github\tushare\tushare\stock\cons.py tushare.stock.cons line: 1, col: 0 Returned location: < Definition module cons >

I really don't know why. does the elpy hava the debug message switch?