Skip to content

Python virtual environment on Intellij IDEA

create python virtual environment post logo

This guide will show you how to create a Python virtual environment on Intellij IDEA and activate it on Intellij’s terminal.

Requirements

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:

Intellij IDEA Project Structure window

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:

Intellij IDEA Add Python Interpreter dialog

Then. click on Apply.

Now, on the same window, go to the Project tab, and assign the newly created virtual environment as the SDK:

Intellij IDEA dialog to assign project SDK to python virtual environment

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:

Intellij IDEA Terminal tab displaying venv prefix

References

Leave a Reply

Your email address will not be published. Required fields are marked *