This guide will show you how to create a Python virtual environment on Intellij IDEA and activate it on Intellij’s terminal.
Requirements
- pyenv – click here to follow to tutorial.
Installing Python with pyenv
We first need to make a Python interpreter available so we can use it in our virtual environment, so let’s install it with pyenv.
Run the command below to print a list of available Python versions.
pyenv install --list Available versions: 2.1.3 2.2.3 ... 3.10.5
For this example, I copied the version 3.10.5
so I can now install it:
pyenv install 3.10.5
Create and activate the Python virtual environment on Intellij IDEA
Now open your project on Intellij and right click on your project name in the Project tab -> Open Module Settings. Then, select the SDK tab:
Next, click on the plus “+” button and select Add Python SDK…
There are a few settings we need to change in the new window that opened. First, make sure you selected the tab “Virtualenv Environment” and edit the following fields as described:
- Pick New environment
- Location:
<your_project_path>/venv
- Base interpreter:
<your_home_folder>/.pyenv/versions/3.10.5/bin/python
Your setting should look similar to below:
Then. click on Apply.
Now, on the same window, go to the Project tab, and assign the newly created virtual environment as the SDK:
Click OK to close the dialog.
To confirm everything worked, you should see your Intellij IDEA terminal prefixed with (venv)
when you open a new terminal tab:
References
- GitHub – Simple Python version management