PyCharm project cannot add poetry interpreter
Asked Answered
G

3

7
  • OS: win10
  • PyCharm version: PyCharm Professional 2021.2.2
  • Poetry version: 1.1.13
  • Poetry plugin version: 1.1.5-212 (from koudai aono)

I have tried to build a new PyCharm project by poetry environment, enter image description here while setting up it showed enter image description here

and cannot setup the interpreter. Have anyone got the similar problem before and know how to solve this error?

Update
I have upgraded the PyCharm version to PyCharm Professional 2022.1.1 and the problem still remain.

Grief answered 20/5, 2022 at 2:49 Comment(2)
What happens when you execute that command manually from a terminal?Phyllode
@Brian I tried poetry env info -p in the project dir and it showed nothing, and poetry env info return the path which is global interpreterGrief
G
9

Alright, I have fixed this problem.

Below is my debug steps, hope it can help those who are struggling in the same situation:

  1. Posting a support report to PyCharm team with no responce
  2. Searching for a lot of posts from communities
  3. Creating a poetry project by terminal
  4. Inside the project directory, I tried poetry env info and it showed that the local virtualenv is NA
  5. Trying to create one by poetry env use $(which python), yet it returned Skipping virtualenv creation, as specified in config file and this answer gave me a hint.
  6. Typing poetry config --list and it showed that virtualenvs.create = false
  7. Trying to enable the virtualenv creation by the command poetry config virtualenvs.create true
  8. Restarted the PyCharm IDE and tried to add the interpreter again and it WORKED!!

I am not sure if the command poetry config virtualenvs.create true is permant or not.

Grief answered 20/5, 2022 at 5:56 Comment(4)
In step 4, I didn't see NA. It shows (vdocv) dev@npl:(old_repo_moving_process/doc-parser-build) % poetry env info Virtualenv Python: 3.9.15 Implementation: CPython Path: /Users/nk/Library/Caches/pypoetry/virtualenvs/doc-parser-svc-yP2QnEEJ-py3.9 Valid: False System Platform: darwin OS: posix Python: /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9_Fenderson
even the create was true. Still the same error. Doesn't work even after restarting pycharmFenderson
You have to recreate the environment in PyCharm - another reason why this can happen is that the name of the package has changed in pyproject.toml, which means that poetry and pycharm no longer agree on what the correct environment is.Anole
Even after fixing the poetry configs I was still getting the same error. Recreating a new python interpreter in pycharm, after fixing the poetry config worked as suggested by @AnoleReflux
B
3

This happened to me when using pyenv and having selected a local Python for that project directory different from the one used to install Poetry. So, this makes pythons local (Pyenv set for project) adn poetry selected (the one used to install) are not found by PyCharm.

Solution

Change Poetry's config (in my case global, but you can by project) to take always the active Python instead of the Poetry installed one.

  1. Check the config list for Poetry poetry config --list
  2. Look for the item virtualenvs.prefer-active-python(default is false)
  3. Set it to true with poetry config virtualenvs.prefer-active-python true

Check that took effect repeating step 1.

Try again on PyCharm.

Benham answered 13/6 at 5:33 Comment(2)
That worked for me, thanks! but in a general view, I think this is a Pycharm issue rather than a Poetry issue: youtrack.jetbrains.com/issue/PY-66449/…January
That's what I believe too. From the top of my head I remember have seen in poetry docs that is in Beta this config to allow using a diff python from the one you've installed poetry with. So probably a Pycharm issue would be the better guess if we consider it's a non stable Poetry feature. I didn't mention it in my solution given it's not relevant. On top of that, it's a VERY good feature: IMO there is no case in having a multiversion env supporter if you're gonna have issues with the versions you didn't use to install it.Benham
R
0

The suggestion by @Natacha is helpful, but didn't complete the job for me. My own personal fails had to do with accepting Pycharm recommendations for the location of the poetry and python binaries. Be sure to get them right! In the Pycharm terminal (in my case, Gitbash):

# Location of python from pyenv     
pyenv which python
# Location of poetry binary 
which poetry

Paying attention to the details saves a lot of grief :)

Robbi answered 7/8 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.