Python

Python virtual environment on Intellij IDEA

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:

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

bgasparotto

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

2 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

2 years ago

Create aliases for commands on Unix

This tutorial will show how to create aliases for commands on Unix based systems, so…

4 years ago