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

Work with poetry. #1

Open
arkhan opened this issue Dec 27, 2019 · 16 comments
Open

Work with poetry. #1

arkhan opened this issue Dec 27, 2019 · 16 comments

Comments

@arkhan
Copy link

arkhan commented Dec 27, 2019

Greetings, great job.
I currently use pyenv + pyenv-virtualenv + poetry for my developments with python,

In emacs I use auto-virtualenv to use the different versions and virtual environments of Python.

Thanks a lot

@aiguofer
Copy link
Owner

Ahh very cool!! I wasn't aware of that project, but it seems to do something quite similar to this. The main difference I see is that it also looks for the virtualenvs in ~/.virtualenvs and /path/to/current_project/.venv.

I haven't used Poetry much.. which of the above methods does it use? I think I could extend this extension to accommodate for those use cases.. or maybe make some PRs to that extension to support pyenv-version-alias usage. Seems a little redundant to have both.

@arkhan
Copy link
Author

arkhan commented Dec 28, 2019

I currently use pyenv + pyenv-virtualenv to develop different python projects on the same version.

With poetry I use it to manage dependencies, but it has a peculiarity that if it does not find an active virtual environment, poetry creates it, omitting the one already managed by pyenv-virtualen.

And with auto-virtualenv or pyevn-mode I activate the virtual environment or python version defined in the root of the project by the .python-version file.

I hope I explained it well

Cheers

@aiguofer
Copy link
Owner

Ahh, I see. Is it related to python-poetry/poetry#1061?

I'll dig a little and see what I can do!

@aiguofer
Copy link
Owner

Ahh nvm, the above is a different issue (when using multiple pyenv versions/virtualenvs in the same project).

The issue seems to be that the VIRTUAL_ENV environment variable isn't being set for the Emacs process when pyenv activates it. I'll see what I can do!

@arkhan
Copy link
Author

arkhan commented Dec 28, 2019

yes, basically I have set up a virtual version / python environment per project

like this:

  • prj1/.python-version
  • prj2/.python-version

@aiguofer
Copy link
Owner

Just to confirm, each of those .python-version only contains 1 virtualenv name right? (pyenv allows chaining versions like version1:version2:version3)

@arkhan
Copy link
Author

arkhan commented Dec 28, 2019

yes, just one python version o virtualenv name per .python-version file

@aiguofer
Copy link
Owner

Try the latest version and let me know if that fixes it!

@arkhan
Copy link
Author

arkhan commented Dec 28, 2019

I just tried it, but the behavior is the same

With elpy
img-2019-12-28-140341

With poetry.el
img-2019-12-28-140616

@aiguofer
Copy link
Owner

Hmm, OK I'll download poetry.el and try to see if I can figure it out.

@aiguofer aiguofer reopened this Dec 28, 2019
@aiguofer
Copy link
Owner

Hmm, wait, did you restart Emacs? When I run elpy-config I see my Virtualenv set correctly.

@aiguofer
Copy link
Owner

for some reason it looks like it's still using system pyenv version, not your local one. Try forcing the env you want with pyenv-use (or pyenv-use-corresponding)

@arkhan
Copy link
Author

arkhan commented Dec 28, 2019

I used pyenv-use-corresponding but I got the same result

@aiguofer
Copy link
Owner

Hmm, so I just tested it and it's working for me. Here's what I did (I already have a version 3.5.7 installed through pyenv):

> mkdir -p ~/work/test
> cd ~/work/test
> pyenv virtualenv 3.5.7 test
> pyenv local test
> poetry init
> poetry env info

Virtualenv
Python:         3.5.7
Implementation: CPython
Path:           /home/aiguofer/.pyenv/versions/3.5.7/envs/test
Valid:          True

System
Platform: linux
OS:       posix
Python:   /home/aiguofer/.pyenv/versions/3.5.7

Then I open up ~/work/test/test.py in emacs and do the following:

M-x pyenv-use-corresponding (this enables the test pyenv virtualenv)
M-x poetry-shell

This is what I see after that:
image

You can see the right pyenv is enabled looking at the bottom right corner of each window, and you can see poetry.el recognized the right virtual environment and activated it.

Also, here's the output of elpy-config when I'm in the ~/work/test/test.py buffer:
image

Again, the right virtualenv is detected there.

I'd like to note that pyenv-use-corresponding works based on the currently active buffer. If you don't have a file in the project open and active, then it won't enable the right pyenv version/virtualenv. I do the following the make sure this is automatic:

(use-package switch-buffer-functions
  :straight t
  :config
  (defun pyenv-update-on-buffer-switch (prev curr)
    (if (string-equal "Python" (format-mode-line mode-name nil nil curr))
        (pyenv-use-corresponding)))

  (add-hook 'switch-buffer-functions 'pyenv-update-on-buffer-switch))

@arkhan
Copy link
Author

arkhan commented Jan 2, 2020

it's weird but it works when using pyenv-use-global

(leaf switch-buffer-functions
  :straight t
  :require t)

(leaf pyenv
  :straight (pyenv
             :type git
             :host github
             :repo "aiguofer/pyenv.el")
  :require t
  :config
  (setq pyenv-use-alias nil
        pyenv-set-path nil)

  (global-pyenv-mode)

  (defun pyenv-update-on-buffer-switch (prev curr)
    (if (string-equal "Python" (format-mode-line mode-name nil nil curr))
        (pyenv-use-global)))
  (add-hook 'switch-buffer-functions 'pyenv-update-on-buffer-switch))

@aiguofer
Copy link
Owner

aiguofer commented Jan 6, 2020

Hmmm interesting, yeah it looks like pyenv.el doesn't really do anything for me if I don't use (global-pyenv-mode). That code was there from when I forked this project... I'll have to read up more on how define-minor-mode works to understand what it's doing.

I personally just use (global-pyenv-mode) anyway, but maybe it makes sense to ensure it works even if you don't enable it permanently.

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

2 participants