Poetry – Python Introduction and Hands-on
Introduction
- Poetry is a tool in python that is used for managing the packages and their dependencies.
- We just need to define the libraries or packages that are required by our projects, and it will manage them for us.
- The current release of Poetry can work on Python 2.7 and 3.5+.
- For the upcoming releases, it will only support Python 3.5+.
- So consider using the python version that is above 3.5.
- All the dependencies can be seen at pyproject.toml file, while this is not available in pip.
- It is very easy to switch between the different versions of python.
- Publishing can be directly done.
Installation
- Poetry has a custom installer that creates a separate environment for all our dependencies.
- Although Poetry can be directly installed, it is preferred to do it by using the curl command.
- For Windows-
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
- For macOS/Linux/Bash-
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
- Let’s check the version of Poetry that is installed, open a new terminal, and then run the poetry –version.
- Installing Poetry using Pip.
pip install --user poetry
- Installing Poetry using pipx.
pipx install poetry
Demonstration
- Let’s create our new project, say – status-poetry
poetry new status-poetry
- Output will be –
- Let’s open our project in the VS Code, and our project will appear something like this.
- pyproject.toml file is the heart of this project.
- If you are coming from the pip background you can consider it as a combination and system and requirement.txt files.
- Readme file is also by-default created, with the test directory.
- Every file will be having a default version of 0.1.0 in them.
- Now the question is can be integrated the Poetry in an existing project?
- Ofcourse yes, you just need to go inside that directory and run –
poetry init
- This will add poetry to your already created project.
- For adding the dependencies we just need to go to our pyproject.toml file.
- There inside the tool.poetry.dependencies we will add our required packages and libraries.
- For example say we want to add pandas 1.3.5 to our project, by this only we can do that.
- Now suppose you don’t want to go the file everytime and update it.
- In this case you can directly add the package by using the CLI and running the command –
poetry add pandas==1.3.5
- By running this command the package will be added automatically to the poetry.lock file.
- Now for installing all the dependencies mentioned in the tool.poetry.dependencies, you just need to run.
poetry install
- It will install all your dependencies.
- For creating a build we can use –
poetry build
- Similarly for publishing the build we can use-
poetry publish
- Now for running the files or script we can use-
poetry run python file.py
- Lets create a file in our project say main.py and run it.
- Or you can add all your scripts to pyproject.toml
[tool.poetry.scripts]
my-script = "my_module:main"
- Then you can run the command –
poetry run my-script
- This will execute your scripts.
References
- https://python-poetry.org/docs/
- Link to my website- Utkarsh Shukla Website
0 Comments
Add Comment
You must be logged in to post a comment.