|
1 |
| -# python-devcontainer-poetry |
| 1 | +# python-devcontainer-poetry |
| 2 | + |
| 3 | +Starter Python project using Poetry with a dev container. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +To use this template, click the "Use this template" button on the GitHub page. This will create a new repository with the same files as this one. |
| 8 | + |
| 9 | +Clone your repository and open it in Visual Studio Code. This allows you to use the devcontainer which includes Python and Poetry. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +Create the source directory and test directory: |
| 14 | + |
| 15 | +```bash |
| 16 | +mkdir <project-name> |
| 17 | +mkdir tests |
| 18 | +``` |
| 19 | + |
| 20 | +Create a new Python file in the source directory: |
| 21 | + |
| 22 | +```bash |
| 23 | +touch <project-name>/__init__.py |
| 24 | +touch tests/__init__.py |
| 25 | +``` |
| 26 | + |
| 27 | +## Setup a project with Poetry |
| 28 | + |
| 29 | +To create a new project with Poetry, run the following command. Use the `project-name` used above to create the directory as the name of the project: |
| 30 | + |
| 31 | +```bash |
| 32 | +poetry init |
| 33 | +``` |
| 34 | + |
| 35 | +Optional: Add pytest to the dev group. You can do this during the init process or by adding the package later: |
| 36 | + |
| 37 | +```bash |
| 38 | +poetry add -G dev pytest |
| 39 | +``` |
| 40 | + |
| 41 | +## Start poetry shell |
| 42 | + |
| 43 | +```bash |
| 44 | +poetry shell |
| 45 | +``` |
| 46 | + |
| 47 | +## Add Dependencies |
| 48 | + |
| 49 | +```bash |
| 50 | +poetry add <package-name> |
| 51 | +``` |
| 52 | + |
| 53 | +## Install Dependencies |
| 54 | + |
| 55 | +```bash |
| 56 | +poetry install |
| 57 | +``` |
| 58 | + |
| 59 | +## Run Tests With Poetry |
| 60 | + |
| 61 | +```bash |
| 62 | +poetry run pytest |
| 63 | +``` |
| 64 | + |
| 65 | +## Run the application |
| 66 | + |
| 67 | +Create a file called `app.py` in the project directory and add the following code: |
| 68 | + |
| 69 | +```python |
| 70 | +def main(): |
| 71 | + print("Hello, world!") |
| 72 | + print("This is the main function.") |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
| 76 | +``` |
| 77 | + |
| 78 | +```bash |
| 79 | +poetry run python <project-name>/app.py |
| 80 | +``` |
| 81 | + |
| 82 | +## Build the Package |
| 83 | + |
| 84 | +```bash |
| 85 | +$ poetry build |
| 86 | +``` |
| 87 | + |
| 88 | +## Useful Poetry Commands |
| 89 | + |
| 90 | +* poetry show —Lists the packages installed in your current project’s virtual environment. You can use `poetry show --tree` to view dependencies in a tree format to help understand the hierarchical structure of package dependencies. |
| 91 | +* poetry add — Add new dependencies to your project. It automatically updates your pyproject.toml and poetry.lock files. |
| 92 | +* poetry install — Reads the pyproject.toml file from the current project, resolves the dependencies, and installs them. If a poetry.lock file exists, it will use the exact versions from there instead of resolving them. |
| 93 | +* poetry env — Shows information about the current environment or even removes virtual environments associated with the project. |
| 94 | +poetry shell— Spawns a shell, like bash or zsh, within the virtual environment created by Poetry. |
| 95 | +* poetry remove— Removes a package that is no longer necessary from the pyproject.toml and lock file. |
| 96 | +poetry version minor— Bumps the minor version of your project (according to semantic versioning). Similar for MAJOR or PATCH . |
0 commit comments