Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No such file or directory" "pyls" #426

Closed
mangelozzi opened this issue Mar 19, 2020 · 8 comments
Closed

"No such file or directory" "pyls" #426

mangelozzi opened this issue Mar 19, 2020 · 8 comments

Comments

@mangelozzi
Copy link

What is the way to work with various python environments. I activate my venv, started emacs, but it still can't find pyls (done as follows):

michael@m2800:~/project$ source venv/bin/activate
(venv) michael@m2800:~/project$ python
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyls
>>> dir(pyls)
['IS_WIN', 'PYLS', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_version', 'hookimpl', 'hookspec', 'os', 'pluggy', 'sys']
>>> 
[9]+  Stopped                 python
(venv) michael@m2800:~/project$ emacs -Q

Then run these commands

(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

M-x package-install RET eglot RET
open a python file
Mx eglot

[eglot] I guess you want to run `pyls', but I can't find `pyls' in PATH!
Enter program to execute (or <host>:<port>): pyls
@nemethf
Copy link
Collaborator

nemethf commented Mar 19, 2020

Until someone proposes a proper solution you can start pyls from the commandline as pyls --tcp -v and start eglot with C-u M-x eglot RET 127.0.0.0:2087 RET

@joaotavora
Copy link
Owner

@michael-angelozzi This problem isn't specific to Eglot. Any Emacs development exercise where you expect to call a Python binary (it may be pyls or something else like the python program interpreter itself) can run into these problems and that's because, by default, Emacs doesn't know that you ran source venv/bin/activate in a particular project.

There are some third-party Emacs packages that fix this (look here maybe), or you could try C-u M-x eglot and point Eglot to the correct path.

So it's not really an Eglot problem. So, unless you demosntrate otherwise, I'm closing this. We can continue discussion, regardless.

@mangelozzi
Copy link
Author

Thanks for the tips @nemethf and @joaotavora
I didn't understand how Language servers work. The issue occurred when I was installing pyls with pip; I used --system-site-packages with venv (pyls installed in the default installation, so it can be shared across projects), and my $PATH for some unknown reason does not include the original system site packages path ~/.local/bin. After activating the venv, and then loading emacs from that shell, it can find pyls (and conversely if I dont activate the venv, then it can't find it) as expected.

@joaotavora
Copy link
Owner

then loading emacs from that shell

Yes, this works, but it's kind of silly to have to run and shutdown an emacs for every project/environment.

So I encourage you to do try one of those alternatives I linked to. And you can post back the results here of the one that worked best for you, and help fellow Pythonistas.

@mangelozzi
Copy link
Author

I generally work on one large project so its no problem for me. The links (and my googling) are for virtualenv, whereas I use the default venv that comes with python3. Still trying to figure out a way to source the activate scripts from emacs.
pyvenv doesn't support venv (just tried and confirmed by jorgenschaefer/pyvenv#86)

@joaotavora
Copy link
Owner

The links (and my googling) are for virtualenv, whereas I use the default venv that comes with python3. Still trying to figure out a way to source the activate scripts from emacs.
pyvenv doesn't support venv

Pity, I'd favour venv too. Well maybe we'll just have to write the thing ourselves...

How does it work, i.e. do you know how run python commands to query the current directory as to what project is loaded, where are libraries and executables?

@falloutphil
Copy link

I prefer venv too as it's core python, but see my comment on jorgenschaefer/pyvenv#86 - I think this should be supported as of Sept 2019. Do people disagree?

@orontee
Copy link

orontee commented Mar 27, 2024

May this help others.

I use the venv module from the standard library, and install I install python-lsp-server in each virtual environment.

Each project with a virtual environment has a .dir-locals.el file with at least something like:

((nil . ((python-shell-virtualenv-root . "/home/matthias/Projets/mopidy-dev/env/"))))

Finally:

(defun my-eglot-python-server-program (&optional interactive project)
  (cond
   (python-shell-virtualenv-root
    `(,(expand-file-name "bin/pylsp" python-shell-virtualenv-root)))
   (t '("pylsp"))))

(add-to-list 'eglot-server-programs
	     '((python-mode python-ts-mode) .
	       my-eglot-python-server-program))

@joaotavora Would a patch with something similar be of any interest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants