#### Guides

# Python

> [!NOTE]
>
> This guide was heavily inspired by [Jack Fields](https://x.com/OrdinaryInds)
> and his
> [article](https://medium.com/ordinaryindustries/the-ultimate-vs-code-setup-for-python-538026b34d94)
> about setting up VS Code for Python development. Please check his article for
> more details.

## Prerequisites

Before you begin, make sure you have:

* [Python](https://python.org) installed (3.8 or higher recommended)
* [Git](https://git-scm.com/) for version control
* Grok Build installed and updated to the latest version

## Essential extensions

The following extensions setup Grok Build to be fully featured for Python development. These provide you with syntax highlighting, linting, debugging and unit testing.

* [**Python**](cursor:extension/ms-python.python) (Core language support from Microsoft)
* [**Cursor Pyright**](cursor:extension/anysphere.cursorpyright) (Fast Python language server)
* [**Python Debugger**](cursor:extension/ms-python.debugpy) (Enhanced debugging capabilities)
* [**Ruff**](cursor:extension/charliermarsh.ruff) (Python linter and formatter)

### Advanced Python tooling

While the above extensions have previously been the most popular extensions for Python development in Grok Build, these additional extensions help you get more out of Python development.

#### `uv` - Python environment manager

[uv](https://github.com/astral-sh/uv) is a modern Python package manager that can be used to create and manage virtual environments, in addition to replacing pip as the default package manager.

To install uv, run the following command in your terminal:

```bash
pip install uv
```

#### `ruff` - Python linter and formatter

[Ruff](https://docs.astral.sh/ruff/) is a modern Python linter and formatter that can be used to check for programming errors, helps enforce coding standards, and can suggest refactoring. It can be used alongside Black for code formatting.

To install Ruff, run the following command in your terminal:

```bash
pip install ruff
```

## Grok Build configuration

### 1. Python interpreter

Configure your Python interpreter in Grok Build:

1. Open Command Palette (Cmd/Ctrl + Shift + P)
2. Search for "Python: Select Interpreter"
3. Choose your Python interpreter (or virtual environment if you're using one)

### 2. Code formatting

Set up automatic code formatting with Black:

> [!NOTE]
>
> Black is a code formatter that automatically formats your code to follow a
> consistent style. It requires zero configuration and is widely adopted in the
> Python community.

To install Black, run the following command in your terminal:

```bash
pip install black
```

Then, configure Grok Build to use Black for code formatting, by adding the following to your `settings.json` file:

```json
{
  "python.formatting.provider": "black",
  "editor.formatOnSave": true,
  "python.formatting.blackArgs": ["--line-length", "88"]
}
```

### 3. Linting

PyLint checks for programming errors, enforces coding standards, and suggests refactors.

To install PyLint, run the following command in your terminal:

```bash
pip install pylint
```

```json
{
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": true,
  "python.linting.lintOnSave": true
}
```

### 4. Type checking

In addition to linting, MyPy checks for type errors.

To install MyPy, run the following command in your terminal:

```bash
pip install mypy
```

```json
{
  "python.linting.mypyEnabled": true
}
```

## Debugging

Grok Build provides these debugging capabilities for Python:

1. Set breakpoints by clicking the gutter
2. Use the Debug panel (Cmd/Ctrl + Shift + D)
3. Configure `launch.json` for custom debug configurations

## Framework support

Grok Build works with popular Python frameworks:

* **Web Frameworks**: Django, Flask, FastAPI
* **Data Science**: Jupyter, NumPy, Pandas
* **Machine Learning**: TensorFlow, PyTorch, scikit-learn
* **Testing**: pytest, unittest
* **API**: requests, aiohttp
* **Database**: SQLAlchemy, psycopg2
