> ## Documentation Index
> Fetch the complete documentation index at: https://astronomer.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Develop Airflow DAGs locally with PyCharm

<Info>
  This page hasn't yet been updated for Airflow 3. The concepts shown are relevant, but some code may need to be updated. If you run any examples, take care to update import statements and watch for any other breaking changes.
</Info>

This example shows how to set up [PyCharm](https://www.jetbrains.com/pycharm/) for local development with Airflow and the [Astro CLI](/docs/cli/v1.43/overview). Setting up a local development environment allows you to iterate more quickly when developing DAGs by taking advantage of IDE features.

## Before you start

Before trying this example, make sure you have:

* [PyCharm](https://www.jetbrains.com/pycharm/)
* The [Astro CLI](/docs/cli/v1.43/install-cli)
* An Astro project running locally on your computer. See [Getting started with the Astro CLI](/docs/cli/v1.43/get-started-cli)

## Configure the Python interpreter

To develop Airflow DAGs in PyCharm, you need to configure at least one Python interpreter. In this example you configure an interpreter using Docker, which allows you to write Python code and interact with running DAGs from within PyCharm.

1. In your PyCharm preferences go to **Build, Execution, Deployment** >> **Docker** and specify how to connect to the Docker daemon. For more, see [Connect to Docker](https://www.jetbrains.com/help/pycharm/docker.html#connect_to_docker).

2. Go to **Project: \<your-project-name>** >> **Python Interpreter**. Click the **Add Interpreter** button on the right, and then click **On Docker**:

   <Frame>
     <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_interpreter_docker.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=bb4423d12f7c13105f2f1ce2fa2ee289" alt="Interpreter settings" width="2034" height="1469" data-path="images/img/examples/pycharm_local_dev_interpreter_docker.png" />
   </Frame>

3. In the window that appears, select **Pull or use existing** for the **Image** option. In the **Image tag** field, select the Docker image that your Airflow environment is running, then click **Next**.

   <Frame>
     <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_docker_target.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=a9ef70be5a6e5f02ec053cb107fa2e74" alt="Image settings" width="1512" height="1092" data-path="images/img/examples/pycharm_local_dev_docker_target.png" />
   </Frame>

4. Wait for PyCharm to finish pulling the Docker image and then click **Next**.

   <Frame>
     <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_docker_image_pull.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=20ac1e8fd9c43b5ea667822b44fd3f34" alt="Image settings" width="1974" height="1500" data-path="images/img/examples/pycharm_local_dev_docker_image_pull.png" />
   </Frame>

5. Ensure the **System Interpreter** is set to Python 3. By default this path will point to the location of Python 3 on the machine and shouldn't need to be changed. Then, click **Create**.

   <Frame>
     <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_system_interpreter.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=7f97578f201145d0719bf26467606c42" alt="Image settings" width="1862" height="1446" data-path="images/img/examples/pycharm_local_dev_system_interpreter.png" />
   </Frame>

6. On the original **Python Interpreter** setup screen, ensure the Python interpreter is set to the docker image and click **Apply**, followed by **OK**.

   <Frame>
     <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_complete_setup.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=15dd7a4b0eed26187836075e6fa8263c" alt="Image settings" width="1876" height="1450" data-path="images/img/examples/pycharm_local_dev_complete_setup.png" />
   </Frame>

## Write Airflow code with PyCharm

After you configure PyCharm to use the correct interpreter for Airflow, you get multiple advantages like code autocompletion, identifying deprecated or unused imports, and error syntax highlighting.

PyCharm will show you when there are deprecated imports in your project:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_deprecated_import.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=bc7839049e20941d54a7990273c87aea" alt="Deprecated Imports" width="861" height="284" data-path="images/img/examples/pycharm_local_dev_deprecated_import.png" />
</Frame>

It can also alert you when an import is unused:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_unused_import.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=3dc3d41dbc2653612d4daea7c381865c" alt="Unused Imports" width="556" height="77" data-path="images/img/examples/pycharm_local_dev_unused_import.png" />
</Frame>

Like with other Python projects, PyCharm will highlight syntax errors in your Airflow code:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_syntax_highlighting.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=ebdaa01e7c68b4931a93afbac9ad98e5" alt="Syntax Highlighting" width="1038" height="199" data-path="images/img/examples/pycharm_local_dev_syntax_highlighting.png" />
</Frame>

Lastly, here is an example of PyCharm autocompleting code and showing built-in definitions:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_autocomplete.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=4812efcd230a597bbb0b6923d410efa6" alt="Code Autocompletion" width="671" height="95" data-path="images/img/examples/pycharm_local_dev_autocomplete.png" />
</Frame>

## Manage Airflow Docker containers with PyCharm

With PyCharm configured to use the Python interpreter from Docker, you can connect to your Docker containers directly from PyCharm using the **Services** pane. If you don't see this pane, try pressing `cmd+8`.

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_docker_services.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=8dbb678c05ca7e9078919314be61a335" alt="Services" width="704" height="230" data-path="images/img/examples/pycharm_local_dev_docker_services.png" />
</Frame>

From the **Services** pane, start Docker by clicking the green play button. You’ll see the same containers appear as when you run `docker ps` after starting your Astro project locally:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_containers.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=15b11f8d363bdbf76e70d111c4323a98" alt="Containers" width="663" height="376" data-path="images/img/examples/pycharm_local_dev_containers.png" />
</Frame>

View logs for the containers by clicking on `/scheduler`, `/triggerer`, `/webserver`, or `/airflow-dev_2cd823-postgres`. Note that these strings might differ based on where the parent directory for your project is located:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_logs.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=9de72e1b2c671c98ec2e0f03762919cf" alt="Logs" width="1904" height="378" data-path="images/img/examples/pycharm_local_dev_logs.png" />
</Frame>

Run Airflow CLI commands by right clicking on `/scheduler` and selecting **Create Terminal** to bash into the container:

<Frame>
  <img src="https://mintcdn.com/astronomer/dALHYMAz3j7hCvzV/images/img/examples/pycharm_local_dev_cli.png?fit=max&auto=format&n=dALHYMAz3j7hCvzV&q=85&s=84793381230c6bb27825de6f25b038c4" alt="CLI" width="1238" height="370" data-path="images/img/examples/pycharm_local_dev_cli.png" />
</Frame>

## See also

* [Develop with VS Code](/docs/learn/vscode-local-dev)
* [Debug interactively with `dag.test`()](/docs/learn/testing-airflow#debug-interactively-with-dag-test)
