Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To use Poetry to install packages, you need to have Poetry itself installed. If you haven't already installed Poetry, you can do so using the following command:
Please ensure that you have python and curl installed on your system before running these commands. After installing Poetry, follow these steps to use it for managing dependencies:
Initialize a new project with Poetry (if you haven't already):
bash
poetry new my-project
Replace my-project with the name of your project. This will create a new directory with some basic structure and a pyproject.toml file, which is used by Poetry to manage dependencies.
Add a dependency:
To add a new package to your project, use the add command:
bash
poetry add package-name
Replace package-name with the name of the package you want to install. Poetry will resolve the dependencies, download the package, and update the pyproject.toml and poetry.lock files accordingly.
Install dependencies:
If you have a pyproject.toml file with defined dependencies, you can install all of them at once using:
bash
poetry install
This command will install all the dependencies specified in your pyproject.toml file, based on the versions locked in the poetry.lock file to ensure repeatability.
Update a dependency:
To update a specific package, use the update command:
bash
poetry update package-name
Replace package-name with the name of the package you want to update. To update all packages, just run poetry update without specifying a package name.
Remove a dependency:
To remove a package that you no longer need, use the remove command:
bash
poetry remove package-name
Replace package-name with the name of the package you want to remove.
Remember to activate the virtual environment that Poetry has created for your project before running your Python application. You can do this by executing poetry shell or by using poetry run python yourscript.py to run your script within the virtual environment without manually activating it.