'PyCharm with Pyenv

How to use Pyenv virtualenv's with Pycharm 2016.3? In the earlier version of Pycharm, I could easily set up local interpreter to point anything installed on my machine.

My first idea was to add .python-version file on the root of the project. I Pyenv virtualenv installed so this will activate & run the project with correct environment automatically. However, Pycharm still doesn't see the correct interpreter causing it to though import and syntax errors.

How can I select my local Pyenv in new PyCharm 2016.3 I used to be able to set the path as variable now I can only browse the path using drop-down menu. It doesn't seem to show hidden files like default path for pyenv ~./.pyenv/versions{project}.



Solution 1:[1]

In Pycharm version 2017.3, you can go to Pycharm -> Preferences -> Project -> Project Interpreter -> <project_name> -> settings button on the right of the python interpreter text box -> Add local

This will open a new window with virtualenv Environment as one of the options on the left. On Selecting it, you will get an option to make a new virtualenv environment or use an existing virtual environment. Here next to the dropdown text box, you can click "..." and browse to your existing virtualenv created using pyenv and select it. It will select this virtualenv when you start terminal from Pycharm and also use the corresponding python interpreter as set while creating that virtualenv.

enter image description here

Solution 2:[2]

Get pyenv-virtualenv plugin for more project-specialized environments.

Then, create a new environment for project: (assume that we installed python-3.7.1 with pyenv and we'll use it)

$ pyenv virtualenv 3.7.1 projectName-3.7.1

This command generates folder for our environment.

Open pyCharm (v2018.3.1 used):

Create New Project > Existing Interpreter

Now you can type path of your environment:

~/.pyenv/versions/projectName-3.7.1/bin/python3

Then press Create.. That's all.

If there is already exists project:

File > Settings > Project: projectName > Project: Interpreter

Again, you can type path of the environment as like above. So you will see packages installed on this environment.

If you want to use same version of python and environment on the command line, then you must activate the environment with

$ pyenv activate projectName-3.7.1

command.

Note that pyenv virtualenv can activate that environment when entering the folder within the terminal through putting the name of it into your .python-version file as well.

For more command about pyenv-virtualenv you can look for reference sheet.

Solution 3:[3]

Personally, I made the best experiences with using pyenv and pipenv together. So far, I used separate commands for that, rather than using the pyenv-virtualenv plugin, but it should be supported with this hint as well.

My workflow to start a new project:

  1. Create folder and switch into it:
    mkdir new_project ; cd new_project
  2. Set desired local pyenv version:
    pyenv local 3.8.0
  3. Create an empty pipenv virtual environment, using just that local version:
    pipenv --python $(pyenv which python)

Now comes the tricky part: PyCharm is supporting Pipenv as an interpreter, but it doesn't recognize it automatically anymore after the initial interpreter selection (which happens at project initiation / first time opening of the project, automatically).
So - if you just created the new project folder (without PyCharm's .idea/ folder created yet), it will recognize the Pipenv-Virtualenv of the project just fine and set it as a project interpreter, automatically:

new project interpreter

If there is already an .idea/ folder, it's not that easy, since PyCharm's GUI just supports to create a new Pipenv environment, but you still have an option:

  1. Close PyCharm, delete .idea/ folder and reopen the project folder in PyCharm.
    • This will delete other project settings as well, but shouldn't be something too important for a fresh environment.
  2. Open the folder in PyCharm again and it will recognize your Pipenv virtualenv.

Solution 4:[4]

You don’t mention what operating system you’re using, and it’s relevant here.

If it’s OS X or macOS, you can press Shift+Cmd+G in the file selection dialog (when you’re choosing the location of a new local interpreter) to enter a path manually. (This is a standard macOS shortcut that works in any native file selection dialog.)

Solution 5:[5]

After taking a lead from Mr. Judge regarding the use of pyenv, I stumbled on a way to introduce an interpreter from pyenv to an existing PyCharm (2020.2.2, if it matters) project without blowing away the .idea directory.

Prior to using any other Environment type (Pipenv, Poetry, etc.), first open the Virtual Environment option: Add Python Interpreter Dialog Select Exiting environment and then navigate to one of your pyenv shims using the […] button to the right of the Interpreter: drop-down. Then click Make available to all projects.

You can then go to the Pipenv or Poetry Environemnt (Plugin) to reference that introduced interpreter now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 OuttaSpaceTime
Solution 3 Judge
Solution 4 wjv
Solution 5