Table of Contents

Poetry is a dependency management tool for Python that handles packaging and dependency management in a unified way. It simplifies tasks like managing libraries, creating virtual environments, and publishing packages. we will learn to install poetry on mac. Poetry has many important benefits like

  • Simplified dependency management: It uses pyproject.toml as a standard to declare dependencies.
  • Easy virtual environments: Poetry automatically creates and manages virtual environments for you.
  • Automated packaging: Poetry makes it easy to package and publish your Python projects to PyPI.

Prerequisite

pipx must be installed on your machine.

  • pipx is designed to install Python applications that have CLI entry points, in isolated environments, so they don’t pollute your main Python environment.
  • Since Poetry is a CLI based tool for python dependency management so you need pipx to install it.
Bash
pipx --version

Install Poetry on Mac

You can directly install poetry on mac using pipx tool. The newly installed version of poetry will be automatically added to your path and you can access it from anywhere

Bash
pipx install poetry

You should see a detailed log of poetry being installed on your machine. you need to install the shell plugin to activate the virtual environment. This is needed for Poetry >= 2.0.0

Bash
poetry self add poetry-plugin-shell

Verify Poetry Installation on Mac

You can use the version command on Poetry tool to check if installation is successfully done or not

Bash
poetry --version

You should see the current version of Poetry printed to your terminal.

Create new Poetry Project in mac

Now lets learn to create a new poetry project using the Poetry CLI tool. we will also install some python modules or libraries and see how they are managed by the Poetry

You can use the new command on poetry to create a new project. let’s say we want to create user-management project

Bash
poetry new user-management

This will create a new directory in the current folder where you ran the command. If you list the contents of the new created project then you will find below files

directory structure of poetry project

What poetry new command does

  • Initializes a new Python package project
  • Creates a pyproject.toml file (used for dependencies & build config)
  • Creates the source package and test skeleton

if you see the contents of pyproject.toml file then you will find below contents

Bash
  user-management cat pyproject.toml
[project]
name = "user-management"
version = "0.1.0"
description = ""
authors = [
    {name = "codewithrajranjan",email = "selftuts@gmail.com"}
]
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
]

[tool.poetry]
packages = [{include = "user_management", from = "src"}]


[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

Activating the Poetry virtual environment

you need to activate the Poetry virtual environment to install libraries specific to a project. If you don’t do this then every project will share the same library and this will be very difficult to manage

Bash
poetry shell

You’ll see extra text in your terminal’s prompt, indicating that the Poetry shell has been activated.

Bash
(user-management-py3.12) ➜  user-management

Add a new dependency using Poetry

Let’s say we want to add the requests library for our project. We will use the add command

As we have activated the poetry shell so all the libraries will be installed in a separate virtual environment

Bash
poetry add requests

The newly installed library or module will be added to the dependencies section of your pyproject.toml file

Bash
(user-management-py3.12) ➜  user-management cat pyproject.toml
[project]
name = "user-management"
version = "0.1.0"
description = ""
authors = [
    {name = "codewithrajranjan",email = "selftuts@gmail.com"}
]
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "requests (>=2.32.4,<3.0.0)"
]

[tool.poetry]
packages = [{include = "user_management", from = "src"}]


[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

You can see “requests (>=2.32.4,<3.0.0)” text inside the dependencies key

This is how you install Poetry in mac and use it to add modules or libraries to your project

References

Leave a Reply

Your email address will not be published. Required fields are marked *