How to publish console utility on PyPI in 1 minute
- Transfer
- Tutorial
Having developed a console utility, you decide to publish it on PyPI. Well, really, what could be better than making it available through pip install ? Googling what is needed for this, you might stumble upon the only post I found on a topic that, not only from 2014, also requires you to create a bunch of folders and files for completely unnecessary (you) things.
How to solve this problem without too much headache in 2019? I already asked this question and therefore, having read a ton of documentation, I created this tutorial for you. Here is a step-by-step guide.
1. Create an account on PyPI ( registration link )
2. Create an entry point to the application (for example, an entry.py file with the following contents)
def main():
print("It's alive!")
3. Install poetry
curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
source $HOME/.poetry/env
(You can install poetry differently, for example, pip install --user poetry - approx. transl.)
4. Create an environment
cd myproject # Перейдите в папку проекта, который будем паковать
poetry init
5. Configure the console command (to do this, add the following lines to the pyproject.toml file that appears )
[tool.poetry.scripts]
APPLICATION-NAME = 'entry:main'
APPLICATION-NAME must be changed to the name of the console command.
6. Publish the utility! (use the username and password from step 1)
poetry publish --username PYPI_USERNAME --password PYPI_PASS --build
Done! Now the other person needs only two commands to install:
$ sudo pip install PROJECT-NAME
$ APPLICATION-NAME
Where PROJECT-NAME is the name that you gave the project in step 4, and APPLICATION-NAME is the name of the team from step 5.
Version update
Whenever you want to update a module, just change the version number in the pyproject.toml file :
version = "0.1.0"
And repeat step 6:
poetry publish --username PYPI_USERNAME --password PYPI_PASS --build
Bonus: customize Travis for auto-publishing
Add the following lines to the .travis.yml file
language: python
dist: xenial
before_install:
- curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
- source $HOME/.poetry/env
install:
- poetry install
script:
- poetry build
deploy:
- provider: script
skip_cleanup: true
script: poetry publish --username $PYPI_USER --password $PYPI_PASS
on:
branch: master
python: '3.7'
tags: true
And set the environment variables PYPI_USER and PYPI_PASS to travis-ci.com . After that, you can publish the package using the commands:
git tag -a v1.2 # Replace version number with yours
git push --tags