Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran1689 <[email protected]>
  • Loading branch information
Kiran1689 committed Nov 29, 2024
1 parent fb366c4 commit 9a3e2ef
Showing 1 changed file with 86 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Setting Up a Python Dev Environment with Dev Containers and Daytona"
description: "A step-by-step guide to setting up a Python dev environment with Dev Containers and Daytona"
description: "A step-by-step guide to set up a Python dev environment with Dev Containers and Daytona"
date: 2024-11-21
author: "Kiran Naragund"
tags: ["python", "devcontainers", "daytona"]
Expand All @@ -10,9 +10,14 @@ tags: ["python", "devcontainers", "daytona"]

## Introduction

In modern Python development, managing dependencies, Python versions, and conflicting libraries can often be cumbersome and time-consuming. Traditional tools like venv, virtualenv, and pipenv have helped manage these issues, but there's a better way to simplify and streamline your workflow using Dev Containers with Daytona.
In modern Python development, managing dependencies, Python versions,
and conflicting libraries can often be cumbersome and time-consuming.
Traditional tools like venv, virtualenv, and pipenv have helped manage
these issues, but there's a better way to simplify and streamline your
workflow using Dev Containers with Daytona.

This guide walks you through setting up a Python development environment using Daytona's containerized workspaces and the Dev Container.
This guide walks you through setting up a Python development environment
using Daytona's containerized workspaces and the Dev Container.

### TL;DR

Expand All @@ -23,14 +28,20 @@ This guide walks you through setting up a Python development environment using D
- Conclusion

## Prerequisites

To follow this guide, you'll need to have the following:
- Basic understanding of [Python](definitions/20240820_defintion_python.md) and [Development Environment](definitions/20240819_definition_development environment.md)
- Basic understanding of [Python](definitions/20240820_defintion_python.md)
and [Development Environment](definitions/20240819_definition_development environment.md)
- An IDE (like [VS Code](https://code.visualstudio.com/))
- Docker (download from [here](https://www.docker.com/))
- Daytona latest version [install from [here](https://www.daytona.io/docs/installation/installation/)]

## Overview of Daytona
Daytona is an open-source development environment manager that uses configuration files from a project's repository to build and provision workspaces. It simplifies the process of setting up consistent development environments across teams or for individual developers.

Daytona is an open-source development environment manager that uses configuration
files from a project's repository to build and provision workspaces. It simplifies
the process of setting up consistent development environments across teams or for
individual developers.

**Key Features of Daytona:**

Expand All @@ -46,7 +57,10 @@ For more info about Daytona and it's features, check [here](https://daytona.io)

## Overview of Dev Containers

Dev Containers, short for Development Containers, are a way to configure portable and reproducible development environments using Docker containers. Dev containers are isolated, lightweight environments that allow developers to work inside a containerized version of a build environment.
Dev Containers, short for Development Containers, are a way to configure portable
and reproducible development environments using Docker containers. Dev containers
are isolated, lightweight environments that allow developers to work inside a
containerized version of a build environment.

**Key Features of Dev Containers:**

Expand All @@ -62,19 +76,21 @@ For more info about Dev Containers and it's features, check [here](https://conta

### Fork the Template Repository

We’ll use this [pthon-project-template](https://github.com/pamelafox/python-project-template), which includes a basic `main.py` and tests in `tests/main_test.py`. Refer to the `README` for details on the project structure.
We’ll use this [pthon-project-template](https://github.com/pamelafox/python-project-template),
which includes a basic `main.py` and tests in `tests/main_test.py`. Refer to the `README` for
details on the project structure.

Click on the "Fork" button at the top right of the repository page to create a local copy under your GitHub account.
Click on the "Fork" button at the top right of the repository page to create a local copy
under your GitHub account.

The `.devcontainer/devcontainer.json` file content defines a configuration for a python development container environment.
The `.devcontainer/devcontainer.json` file content defines a configuration for a python
development container environment.

```json
{
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",

"customizations": {

"vscode": {
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
Expand All @@ -86,7 +102,6 @@ The `.devcontainer/devcontainer.json` file content defines a configuration for a
"__pycache__": true
}
},

"extensions": [
"ms-python.python",
"charliermarsh.ruff",
Expand All @@ -95,45 +110,50 @@ The `.devcontainer/devcontainer.json` file content defines a configuration for a
}
},


// "forwardPorts": [],

"postCreateCommand": "git config --global --add safe.directory ${containerWorkspaceFolder}",

"postStartCommand": "pip3 install --user -r requirements-dev.txt && pre-commit install",

"remoteUser": "vscode"
"remoteUser": "vscode"
}
```

Let's breakdown the `devcontainer.json` file
- **name**: Specifies the name of the development environment.
- **image**: Points to the Docker image used for the container, `mcr.microsoft.com/devcontainers/python:3.11-bullseye`, a Python 3.11 environment based on Debian Bullseye.
- **customizations**: Allows customization of the development environment, specifically for Visual Studio Code.
- **vscode**: AContains VS Code-specific configurations
- **settings**: Defines default VS Code settings for the container:
- **python.defaultInterpreterPath**: Specifies the Python interpreter path inside the container (`/usr/local/bin/python`).
- **python.testing.pytestEnabled**: Enables the use of `pytest` for testing.
- **python.testing.unittestEnabled**: Disables unittest as the testing framework.
- **files.exclude**: Hides specified files and folders (e.g., `.coverage`, `.pytest_cache`, `__pycache__`) from the VS Code file explorer.
- **extensions**: Lists extensions to be installed automatically in the container:
- **ms-python.python**: Python language support for VS Code.
- **charliermarsh.ruff**: A Python linter.
- **ms-python.black-formatter**: Formatter for Python code using Black.
- **forwardPorts**: (Currently commented out) Would be used to expose specific container ports to the host machine for interaction.
- **postCreateCommand**: Configures Git to recognize the container workspace folder as safe (`git config --global --add safe.directory ${containerWorkspaceFolder}`).
- **postStartCommand**: Installs Python packages from requirements-dev.txt (`pip3 install --user -r requirements-dev.txt`) and sets up pre-commit hooks (`pre-commit install`).
- **remoteUser**: sets `vscode` as the non-root default user.

By including a devcontainer.json file in your project repository, you can specify not just the Python version and dependencies, but also any required system packages, VS Code extensions, environment variables, and even custom scripts to run during setup.

- **name**: Specifies the name of the development environment.
- **image**: Points to the Docker image used for the container, `mcr.microsoft.com/devcontainers/python:3.11-bullseye`, a Python 3.11 environment based on Debian Bullseye.
- **customizations**: Allows customization of the development environment, specifically for Visual Studio Code.
- **vscode**: AContains VS Code-specific configurations
- **settings**: Defines default VS Code settings for the container:
- **python.defaultInterpreterPath**: Specifies the Python interpreter path inside the container (`/usr/local/bin/python`).
- **python.testing.pytestEnabled**: Enables the use of `pytest` for testing.
- **python.testing.unittestEnabled**: Disables unittest as the testing framework.
- **files.exclude**: Hides specified files and folders (e.g., `.coverage`, `.pytest_cache`, `__pycache__`) from the VS Code file explorer.
- **extensions**: Lists extensions to be installed automatically in the container:
- **ms-python.python**: Python language support for VS Code.
- **charliermarsh.ruff**: A Python linter.
- **ms-python.black-formatter**: Formatter for Python code using Black.
- **forwardPorts**: (Currently commented out) Would be used to expose specific container ports to the host machine for interaction.
- **postCreateCommand**: Configures Git to recognize the container workspace folder as safe (`git config --global --add safe.directory ${containerWorkspaceFolder}`).
- **postStartCommand**: Installs Python packages from requirements-dev.txt (`pip3 install --user -r requirements-dev.txt`) and sets up pre-commit hooks (`pre-commit install`).
- **remoteUser**: sets `vscode` as the non-root default user.

By including a devcontainer.json file in your project repository, you can specify not just
the Python version and dependencies, but also any required system packages, VS Code extensions,
environment variables, and even custom scripts to run during setup.

## Step 2: Main Process

### Step 2.1: Start Daytona Server

Start the daytona server by running the command

```bash
daytona server
```

Your output should be similar to the screenshot below.
![Start Daytona Server](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img1.png)

Expand All @@ -142,9 +162,13 @@ Choose "yes" and you should see a similar output in the screenshot below.

### Step 2.2: Add Git Provider

Daytona integrates with your preferred Git provider, streamlining your workflow by allowing direct access to repositories, and simplifying workspace creation from existing projects.
Daytona integrates with your preferred Git provider, streamlining your workflow
by allowing direct access to repositories, and simplifying workspace creation
from existing projects.

Execute the command provided below to add your git provider. Daytona also has support for other git providers like Bitbucker and Gitlab. You can learn more about Daytona Git Providers [here](https://www.daytona.io/docs/configuration/git-providers/)
Execute the command provided below to add your git provider. Daytona also has
support for other git providers like Bitbucker and Gitlab. You can learn more
about Daytona Git Providers [here](https://www.daytona.io/docs/configuration/git-providers/)

```bash
daytona git-provider add
Expand All @@ -161,29 +185,39 @@ Select GitHub and provide your personal access token.
### Step 2.3: Choose your preferred IDE
Run this command in terminal to choose your [IDE](https://www.daytona.io/docs/usage/ide/).

```bash
daytona ide
```

![preferred IDE](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img5.png)

### Step 2.4 Create a Daytona Workspace
Now create a dev environment of the repository you created in GitHub(by forking) and follow the prompts after you run it.

Now create a dev environment of the repository you created in GitHub(by forking) and
follow the prompts after you run it.

```bash
daytona create
```

Choose Github as provider and select the python-project-template repository.

#### Step 2.4.1 Provide workspace name
The name of the workspace is usually the repository name if you didn't modify it when prompted in the creation of the workspace. In my case, it's `python-project-template`
#### Step 2.4.1 Provide workspace name

The name of the workspace is usually the repository name if you didn't modify it when
prompted in the creation of the workspace. In my case, it's `python-project-template`

![workspace name](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img6.png)

#### Step 2.4.2 Choose the target
Now it will ask you to choose the target, select `local` and enter.
![workspace name](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img6.png)

![Choose the target ](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img7.png)

Daytona will now start creating a workspace by pulling your project and install all required dependencies, once done it will open your project in your default IDE.
#### Step 2.4.2 Choose the target

Now it will ask you to choose the target, select `local` and enter.

![Choose the target ](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img7.png)

Daytona will now start creating a workspace by pulling your project and install all
required dependencies, once done it will open your project in your default IDE.

![Daytona workspace](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img8.png)

Expand All @@ -192,7 +226,9 @@ Daytona will now start creating a workspace by pulling your project and install
Now, your python dev environmentis ready to use.

### Step 3: Confirmation
In your VS Code, run the test cases using the command below, you will see all the testcases passed. This means the python dev environment has successfull set for your project.

In your VS Code, run the test cases using the command below, you will see all the testcases passed.
This means the python dev environment has successfull set for your project.

```bash
python3 -m pytest
Expand All @@ -201,7 +237,10 @@ python3 -m pytest
![Daytona workspace](assets/setting_up_a_python_dev_environment_with_devcontainers_and_daytona_img10.png)

## Conclusion
By following this guide, you've set up a fully containerized Python development environment using Daytona and Dev Containers. This setup ensures that your projects are reproducible, isolated, and consistent across machines, enabling smooth collaboration and effortless debugging.

By following this guide, you've set up a fully containerized Python development environment using
Daytona and Dev Containers. This setup ensures that your projects are reproducible, isolated, and
consistent across machines, enabling smooth collaboration and effortless debugging.

## References

Expand Down

0 comments on commit 9a3e2ef

Please sign in to comment.