Skip to content

Python Development Setup in IntelliJ IDEA and PyCharm

Published:

Table of contents

Open Table of contents

Description

This guide provides step-by-step instructions on how to set up Python with Conda environment in IntelliJ IDEA and PyCharm, along with setting up Black, isort, Pylint, and mypy for efficient coding. You’ll learn how to create a Python environment, install necessary tools, and configure the IDE for automatic formatting and linting.

1. Install Conda and Set Up Python Environment

Install Conda

If you haven’t already installed Conda, download and install Anaconda or Miniconda from Conda’s website.

Create a Conda Environment (optional)

In your terminal, create a new Conda environment with Python:

conda create -n myenv python=3.11

Replace myenv with your desired environment name.
You can also create a new Conda environment directly from the IDE.

2. Configure Python Environment in IntelliJ IDEA/PyCharm

Configure Python Interpreter

Install Intellij Plugins for Pylint and mypy (optional)

To enable more integrated support:

3. Activate Python Environment and Install Required Tools

Activate Conda Environment

conda activate myenv

After activating your Conda environment, install the necessary packages.

Install Required Dependencies from environment.yml

This command will install any dependencies listed in environment.yml if you already have one

conda env update -f environment.yml

Install Black (if needed)

conda install black

Install isort (if needed)

conda install isort

Install Pylint (if needed)

conda install pylint

Install mypy (if needed)

conda install mypy

Install nbqa (if working with Jupyter Notebooks)

conda install conda-forge::nbqa

Save New Dependencies to environment.yml

conda env export --no-builds > environment.yml

4. Configure Black and isort to Run on Save

Both Black and isort will automatically format code on file save.

Configure Black

This setup enables automatic code formatting with Black each time you save a Python file.

  1. Go to File > Settings > Tools > File Watchers
  2. Click + to add a new watcher, select <custom>
    • File Type: Python
    • Scope: Project Files
    • Program: Path to Black (usually $PyInterpreterDirectory$/black)
    • Arguments: $FilePath$
    • Output paths to refresh: $FilePath$
    • Working directory: $ProjectFileDir$

If you are working with Jupyter Notebooks, you can use nbpq utility to format your notebooks using black, with File Watcher configured as follows:

Configure isort

  1. Add another File Watcher for isort similar to the steps above
    • File Type: Python
    • Scope: Project Files
    • Program: Path to isort (usually $PyInterpreterDirectory$/isort)
    • Arguments: $FilePath$
    • Output paths to refresh: $FilePath$
    • Working directory: $ProjectFileDir$
  2. Add .isort.cfg to the project root, example:
[tool.isort]
profile = "black"

If you are working with Jupyter Notebooks, you can use nbpq utility to format your notebooks using isort, with File Watcher configured as follows:

5. Configure Pylint and mypy to Run on Program Start

Both Pylint and mypy will be automatically executed on program startup.

Configure Pylint

  1. Go to Run > Edit Configurations > Your configuration
  2. Enable before launch tasks by selecting Modify Options > Add before launch task
  3. Click + to add a new task, select Run External tool:
    • Program: Path to Pylint (usually $PyInterpreterDirectory$/pylint)
    • Arguments: $ProjectFileDir$/src
    • Working directory: $ProjectFileDir$
  4. Add .pylintrc to the project root, example:
[MASTER]
disable=
    missing-module-docstring,
    missing-function-docstring,
    missing-class-docstring

Configure mypy

  1. Add another task for mypy similar to the steps above
    • Program: Path to mypy (usually $PyInterpreterDirectory$/mypy)
    • Arguments: $ProjectFileDir$/src
    • Working directory: $ProjectFileDir$
  2. Add mypy.ini to the project root, example:
[mypy]
# Disallow dynamic typing
disallow_any_unimported = True
disallow_any_expr = True
disallow_any_decorated = True
disallow_any_generics = True
disallow_any_explicit = True
disallow_subclassing_any = True

# Disallow untyped definitions and calls
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True

# None and optional handling
no_implicit_optional = True

# Configuring warnings
warn_unused_ignores = True
warn_no_return = True
warn_return_any = True
warn_redundant_casts = True

# Misc things
strict_equality = True

# Config file
warn_unused_configs = True

That’s everything you need to configure Python development in JetBrains IDEs. Happy coding!
All File Watchers in .xml format can be found here.