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
- Go to File > Project Structure > Project > SDK and choose Add SDK > Python SDK…
- Choose Conda Environment and select the
myenv
environment created earlier OR create a new environment
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.
- Go to File > Settings > Tools > File Watchers
- 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$
- File Type:
If you are working with Jupyter Notebooks, you can use nbpq
utility to format your notebooks using black
, with File Watcher configured as follows:
- File Type:
Jupyter Notebook
- Scope:
Project Files
- Program: Path to nbpq (usually
$PyInterpreterDirectory$/nbpq
) - Arguments:
black $FilePath$
- Output paths to refresh:
$FilePath$
- Working directory:
$ProjectFileDir$
Configure isort
- 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$
- File Type:
- 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:
- File Type:
Jupyter Notebook
- Scope:
Project Files
- Program: Path to nbpq (usually
$PyInterpreterDirectory$/nbpq
) - Arguments:
isort $FilePath$
- Output paths to refresh:
$FilePath$
- Working directory:
$ProjectFileDir$
5. Configure Pylint and mypy to Run on Program Start
Both Pylint
and mypy
will be automatically executed on program startup.
Configure Pylint
- Go to Run > Edit Configurations > Your configuration
- Enable before launch tasks by selecting Modify Options > Add before launch task
- Click + to add a new task, select Run External tool:
- Program: Path to Pylint (usually
$PyInterpreterDirectory$/pylint
) - Arguments:
$ProjectFileDir$/src
- Working directory:
$ProjectFileDir$
- Program: Path to Pylint (usually
- Add
.pylintrc
to the project root, example:
[MASTER]
disable=
missing-module-docstring,
missing-function-docstring,
missing-class-docstring
Configure mypy
- Add another task for
mypy
similar to the steps above- Program: Path to mypy (usually
$PyInterpreterDirectory$/mypy
) - Arguments:
$ProjectFileDir$/src
- Working directory:
$ProjectFileDir$
- Program: Path to mypy (usually
- 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.