92

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}.

Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63
  • 1
    Using a single Python interpreter is still easy to specify (see below), but how do I specify all interpreters (i.e. all of which are listed by `pyenv global`)? I need this to test against several versions when I use [Tox](https://tox.readthedocs.io/en/latest/). This would require to somewhat run `source <(pyenv init -)` or similar, [according to the docs](https://github.com/pyenv/pyenv#installation), in the shell that runs `tox`. – Peterino Sep 16 '18 at 06:30

6 Answers6

147

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

Viral Modi
  • 1,957
  • 1
  • 9
  • 18
  • 1
    Just accepted your answer as its better than the existing one. – Kimmo Hintikka Sep 19 '18 at 12:31
  • 14
    A suggestion for improvement is to use a pyenv python version not as a virtual environment, but rather as a *base interpreter* for a new one. This would let you create multiple virtual environments per python version. – EliadL Oct 08 '18 at 13:31
  • 1
    @EliadL, you can achieve that simply browsing to the .pyenv directory and selecting the relevant `/bin/python` when you select the base interpreter. – Def_Os Feb 15 '19 at 20:14
  • 1
    @Def_Os How is that different from my suggestion? – EliadL Feb 17 '19 at 11:59
  • 1
    @EliadL, I thought you were under the impression that it is not currently possible, hence my instructions. – Def_Os Feb 18 '19 at 18:06
24

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.

OuttaSpaceTime
  • 710
  • 10
  • 24
SEGV
  • 848
  • 1
  • 8
  • 19
  • This is still not enough due to problems pyenv creates which messes up $PATH https://github.com/pyenv/pyenv-virtualenv/issues/274 – oglop Dec 26 '18 at 16:47
  • 1
    This answer in combination with the information from the accepted one was very helpful for me. Thank you! – A. Blesius Nov 29 '19 at 09:15
10

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.
Judge
  • 644
  • 8
  • 11
  • 1
    Thanks for the lead. I was able to figure out how to coerce the introduction of a `pyenv` _shim_ into an existing PyCharm project w/o blowing away the `.idea` directory. My addition is in an answer somewhere on this page. – gkedge Sep 25 '20 at 03:10
  • This works for me. I am using PyCharm 2022.2.1 and python 3.10.7 using pyenv and venv. Thanks a lot. – BC Smith Sep 14 '22 at 22:47
5

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.)

wjv
  • 2,288
  • 1
  • 18
  • 21
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.

gkedge
  • 197
  • 2
  • 11
  • I'm not sure I understand your answer. Do you mean `pipenv` at the begining and not `pyenv`? Also, I don't get why the "shimmed python" is made available as a virtualenv while it is more like a "system interpreter" rather than a virtualenv. – Cedric H. Dec 18 '20 at 21:05
  • That `---` in the interpreter path really threw me off. I finally realized all you have to do is `which python` in terminal, then browse to that version of python in the above PyCharm dialogue. On macOS for me it's `/Users/[redacted-username]/.pyenv/shims/python`. Specifying `python3` for some reason didn't work (PyCharm said `Invalid Interpreter`, but with pyenv, `python` ended up being the same pyenv shim as `python3`, so it didn't matter.) – knightofiam Oct 07 '22 at 02:59
  • I set `/Users/myName/.pyenv/shims/Python` as a Python interpretor. When I switch the version with `global` command it does change instantly when I run `Python -V` but pyCharm selects a versioned path instead of `/Users/AdnanAhmad/.pyenv/shims/Python`. Why is that? – Volatil3 Oct 26 '22 at 15:12
0

The answers from @Judge and @gkedge worked for me, but I ran into a few obstacles setting up a project from a github clone with a requirements.txt file. Here are the steps that worked for me.

First, I had problems with pipenv, so I followed the instructions in the post https://stackoverflow.com/a/74475168/5445739 to install pipenv using python.

Before starting Pycharm, open a terminal and

  • clone the project git clone xyz.git and cd xyz
  • set python version pyenv local 3.11.4
  • activate the pipenv environment python -m pipenv shell
  • install the packages pipenv install -r requirements.txt

Start Pycharm and

  • open the project <path>/xyz
  • select add an existing python interpreter and paste the output of the which python command; in my case this turns out to be /home/<user>/.local/share/virtualenvs/<repo>-r0oIFQby/bin/python

At this point I had a working interpreter along with all the necessary packages. My environment is WSL2 Ubuntu and Pycharm for Windows, version 2022.3.3.

kingaj
  • 611
  • 6
  • 13