PyCharm with Pyenv
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.
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.
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:
- Create folder and switch into it:
mkdir new_project ; cd new_project
- Set desired local pyenv version:
pyenv local 3.8.0
- 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:
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:
- 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.
- Open the folder in PyCharm again and it will recognize your Pipenv virtualenv.