In this blog let’s see how we can package an application and distrubute it using tool called poetry.1
Step 1. Download and Install Poetry
$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python $ source $HOME/.poetry/env
Check the installation correctly by executing
If this throws and error regarding ‘threads’ like the one below
rahul@rahul-lap:~$ poetry --version /home/rahul/.poetry/lib/poetry/_vendor/py2.7/subprocess32.py:149: RuntimeWarning: The _posixsubprocess module is not being used. Child process reliability may suffer if your program uses threads. "program uses threads.", RuntimeWarning) Poetry version 1.0.2
, you might need to change the first line in configuration
#!/usr/bin/env python to
#!/usr/bin/env python3. Save this file and execute
poetry --version again to check for errors if any.
Step 2. Packaging
If you have a simple script written in python, something similar following ‘demo-tool’
def main(): print("Hello World") if __name__ == '__main__': main()
, you have to go the project directory, open up the terminal and execute,
poetry init. This command will guide you to create the most important file called
pyproject.toml, which will orchestrate the packaging for you. Once the interactive generation is finishied, this file will look like below
[tool.poetry] name = "demo-tool" version = "0.1.0" description = "a demo tool for printing hello world" authors = ["Your Name <firstname.lastname@example.org>"] license = "MIT" [tool.poetry.dependencies] python = "^3.7" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"
You can add an entry point to the file using ‘console_script’ in poetry way. Following lines to be appended to our demo program.
[tool.poetry.scripts] demo-tool = 'demo-tool:main'
In case your program has some dependecy, you can add it via
poetry add <package-name>. For example, if your program depends on beautifulsoup webscrapping library, you have to add this dependency to the toml file by running the command
poetry add beautifulsoup4
Step 3. Distributing
Once all your packages are ready to be shipped, you can publish it in PyPI, which is repo of python packages used by the popular tool
Run the following command to publish your package
Then publish it(Here you have to enter your pyPI credentials)
Or you can do it with single command as below
poetry publish --username <YOUR-USERNAME> --password <YOUR-PASSWORD> --build
If you do not have an account in PyPI, go to pypi.org and create and account.
Now that your tool is available to outside world to use.
Step 4. Installation and Usage
Now anyone can install this tool by running,
$ sudo pip3 install demo-tool
This will create a system-wide availability of the command ‘demo-tool’ and can be run by simply executing
demo-tool anywhere from the system.
If you want to locally install it, create a virtual environment using
virtualenv venv and activate it using
source /venv/bin/activate and then run
pip install demo-tool. This will be available locally only to this virtual environment.3