Closed btel closed 3 years ago
Can you run Jedi directly and use the InterpreterEnvironment so that we can have a useful stacktrace? I think that would help.
Ok I guess I'm not even sure anymore if that would help. My guess is that there is something fundamentally broken that you are not allowed to instantiate WindowsPath on your Windows system. I would probably report this somewhere else.
You should probably try to invoke it directly and see if the problem still happens at that point.
Thanks for the quick reply.
In my mind, the problem is that I am running posix python in vim (using msys2 shell), so it expects the posix path. On the other hand, I think jedi runs anaconda python (win64 exeutable, from environment_path) in subprocess and pickles windows path (from pathlib) and it cannot be unpickled in posix python.
When you look into pahtlib code, you find that it uses os.name to determine the platform:
On my system:
Anaconda3 python:
$ winpty /c/Users/btel/Anaconda3/envs/myenv/python
Python 3.8.10 (default, May 19 2021, 13:12:57) [MSC v.1916 64 bit (AMD64)] :: An
aconda, Inc. on win32
>>> import os
>>> print(os.name)
nt
msys2 (posix) python:
$ /usr/bin/python
Python 3.8.7 (default, Jan 15 2021, 14:12:45)
[GCC 10.2.0] on msys
>>> import os
>>> print(os.name)
posix
>>>
so in one case pathlib will create WindowsPath object but then it does not know how to convert it into PosixPath when loading the pickle in vim python.
Does it make sense for you?
I don't know whether this should be reported to python tracker or it might be fixed in jedi (maybe by pickling strings instead of pathlib objects?).
There is a similar issue in fastai: https://github.com/fastai/fastai/issues/1482
@davidhalter I came up with a minimal example showing why this error might occur:
# content of dump_path.py
import pickle
import pathlib
p = pathlib.Path('.')
with open('path.pkl', 'wb') as fid:
pickle.dump(p, fid)
# content of load_path.py
import pickle
with open("path.pkl", 'rb') as fid:
p = pickle.load(fid)
Then:
$ ~/Anaconda3/envs/myenv/python.exe dump_path.py
$ /usr/bin/python.exe load_path.py
Traceback (most recent call last):
File "load_path.py", line 4, in <module>
p = pickle.load(fid)
File "/usr/lib/python3.8/pathlib.py", line 1043, in __new__
raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'WindowsPath' on your system
Do you think this could be happening in jedi (/usr/bin/python is embedded in vim, wherease Anaconda python is on my jedi environment_path)? Can it be fixed in jedi or should I report elsewhere? Thanks!
I kind of understand why this is happening now, thanks for reproducing that. However I feel like this is just too niche of a use case. If anything your VIM should probably just use InterpreterEnvironment
instead of a subprocess. I'm not sure if this is currently supported by jedi-vim, but that should be the way to go. IMO Jedi does it fine here and your use case is just not supported. But the InterpreterEnvironment
should do the job for you.
Thanks for the answer @davidhalter. I appreciate your advice. I will try InterpreterEnvironment
. Just a quick question: does the InterpreterEnvironment
respect the paths of the python from environment? Otherwise, should I retrieve the paths via subprocess and then patch InterpreterEnvironment
with the correct paths?
You can always provide specific paths, but the InterpreterEnvironment
uses the sys path of its running Python process instead of starting a subprocess.
I am running vim on windows in msys2 shell. vim python is executed via msys2-installed python, but the python executable (configured in
environment_path
) is native-windows python from anaconda:When I try do go to definition from vim, I will get the following error:
I think that the error might be caused by jedi pickling a WindowsPath (while running winodws executable) but then it can't be unpickled in vim python which is a posix executable. Would this explain this error? Is there an easy workaround or I should not mix the platforms when running jedi?