Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Mathieu
Mathieu

Posted on • Edited on • Originally published atbrainsorting.dev

     

Publish a package on PyPi using Poetry

You can also read that article on myblog or onmedium !

First thing first, you need Poetry if you don't have it already.

Poetry is the new standard for creating and managing virtual environment for your Python project. It is also a Python dependency management tool that is working differently as pip. It uses the new standardpyproject.toml decided by the Python Packaging Authority with PEP-518. This file merges all the previous config files that were necessary before,setup.py,requirements.txt,setup.cfg,MANIFEST.in etPipfile, in one unique file to rule them all ! Ok, enough of Lord of the Ring.

I advise you to follow along with thedocumentation of poetry open.

Install Poetry

curl-sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
Enter fullscreen modeExit fullscreen mode

Feel free to lookhere for more instructions. Updating Poetry is very easy by the waypoetry update.

Create the package you want to publish on PyPi

Poetry has a nice command that can create your project tree at oncepoetry new <package_name>.

/<package_name>├── README.rst# I personnaly change it to .md as I prefer writing in Markdown├── <package_name>│   └── __init__.py├── pyproject.toml└── tests    ├── __init__.py    └── test_package_name.py
Enter fullscreen modeExit fullscreen mode

You will need to add things to the generatedpyproject.toml file, feel free to check thepoetry documentation to see all the options.

[tool.poetry]name = "vspoetry"version = "0.1.0"description = "Description of your package"authors = ["MattiooFR <dugue.mathieu@gmail.com>"]keywords = ["keyword", "another_keyword"]readme = "README.md"license = "MIT"homepage = "https://github.com/MattiooFR/package_name"repository = "https://github.com/MattiooFR/package_name"include = ["LICENSE",][tool.poetry.dependencies]python = "^3.5"[tool.poetry.dev-dependencies][tool.poetry.scripts]cli_command_name = 'package_name:function'[build-system]requires = ["poetry>=0.12"]build-backend = "poetry.masonry.api"
Enter fullscreen modeExit fullscreen mode

The beginning of the file is pretty straightforward. Lets focus on those three part :

  • [tool.poetry.dependencies]: This is where you can list all the dependencies that your package needs to work. It's like the oldrequirements.txt file.You can do it by hand and then call the commandpoetry install to install them all for your package development and working purposes. If you usepoetry add <dependency_name> (equivalent to thepip install <dependency_name>), poetry will add it there for you.
  • [tool.poetry.dev-dependencies]: If you need development dependencies, that's where they go. Again, you can also install them withpoetry add <dependency_name> --dev (or -D) and poetry will also put that in the right place in yourpyproject.toml file.
  • [tool.poetry.scripts]: This last block is very important if you want your package to have script callable from the terminal.

    script_name = '{package_name}:{function_name}'

By the way, if you still need to have arequirements.txt with all the dependencies (if you use Heroku for example), you can easily have it with this command :

poetryexport-f requirements.txt> requirements.txt
Enter fullscreen modeExit fullscreen mode

Build your package

When your package is ready, simply dopoetry build to create the package files

❯ poetry buildBuilding package_name(0.1.0) - Building sdist - Built package_name-0.1.0.tar.gz - Building wheel - Built package_name-0.1.0-py3-none-any.whl
Enter fullscreen modeExit fullscreen mode

You can test your package by doing

pipinstall <path_to_package_name-0.1.0-py3-none-any.whl>
Enter fullscreen modeExit fullscreen mode

If everything works, congrats ! Now lets publish it to PyPi so others can use your great package too.

Publish your package

Before publishing you should first create an account onPyPi. You can also register an account onTestPyPi if you want publishing on a test repository before trying on the official one.

Your package need to be built, so first runpoetry build if you haven't done it already.

Then simply runpoetry publish and your package will be publish on PyPi :

❯ poetry publishPublishing vspoetry(0.1.0) to PyPIUsername: MattiooPassword: - Uploading vspoetry-0.1.0-py3-none-any.whl 100% - Uploading vspoetry-0.1.0.tar.gz 100%
Enter fullscreen modeExit fullscreen mode

If you need to update your package, simply increment the version in thepyproject.toml file and usepoetry publish (after you build the new package withpoetry build).

Once you have done that you can then install your package :

pipinstall <your_package>poetry add <your_package># but lets be honest now you use Poetry so you would do this !
Enter fullscreen modeExit fullscreen mode

Congrats on publishing your package on PyPi and experiencing the simplicity of using a tool like Poetry.

Happy coding !

End notes

I actually just published my first package,vspoetry, while writing this tutorial. It is a package that add thevspoetry script command to my terminal and this simply add the path of the current poetry virtual environment of the project to VScodesettings.json. This tells to VScode where to grab the project python venv because VScode does not do that automatically yet.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

My name is Mathieu and I'm learning Python to become an awesome machine learning programmer!
  • Location
    Toulouse, France
  • Joined

More fromMathieu

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp