Contents

Upload distribution to PyPI

Environment

  • MacOS Catalina 10.15.7
  • Python 3.9.7

Directory structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
concurrent-api-client
├── .python-version
├── README.md
├── MANIFEST.in
├── requirements.in
├── requirements.testing.in
├── requirements.txt
├── setup.py
├── tox.ini
├── src
│   └── api_client
│       ├── __init__.py
│       ├── api_client.py
│       └── ...
└── tests
    ├── __init__.py
    ├── test_api_formatter.py
    └── ...

Register PyPI account

Email confirmation is required for each environment.


create ~/.pypirc

~/.pypirc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[distutils]
index-servers =
  pypi
  testpypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: [account]
password: [password]

[testpypi]
repository: https://test.pypi.org/legacy/
username: [account]
password: [password]

Install wheel and twine

  • wheel : A built-package format for Python
  • twine : Collection of utilities for publishing packages on PyPI
1
$ pip install wheel twine

Revisit setup() keywords

I have already made setup.py when managed package dependency and configured tox.

Refering setuptools | New and Changed setup() Keywords, updated version and keywords

setup.py

1
2
3
4
    version='0.0.1',
    keywords=[
        'request', 'API', 'REST', 'client', 'async', 'concurrent'
    ],

Create package

Before creating package, it might be better to clean up .egg-info and dist directory that was created last time.

1
$ rm -f -r build/* dist/* .eggs/*

Create distribution

1
$ python setup.py sdist

dist/concurrent-api-client-0.0.1.tar.gz is created.

Create library packages

1
$ python setup.py bdist_wheel

Modules are located to build/lib and lib/concurrent-api-client-0.0.1-py3-none-any.whl is created.


Upload distribution

Upload to test.pypi.org

1
$ twine upload --repository testpypi dist/*

When upload completed, you can check https://test.pypi.org/project/"package-name"/

e.g. https://test.pypi.org/project/concurrent-api-client/

Uplodad to pypi.org

1
$ twine upload --repository pypi dist/*

When upload completed, you can check https://pypi.org/project/"package-name"/

e.g. https://pypi.org/project/concurrent-api-client/


Install package

Install from test.pypi.org

1
$ pip --no-cache-dir install --upgrade --index-url https://test.pypi.org/simple/  concurrent-api-client
Warning
As of 28 Oct 2021, install from test.pypi.org fails because pip could not find a version that satisfies the requirement in test.pypi.org. See Trouble shooting 2.package dependency version unmatched

Install from pypi.org

1
2
3
4
5
6
7
8
$ python -m venv venv-test
$ source venv-test/bin/activate
$ (venv-test) pip  --no-cache-dir install --upgrade install -i https://pypi.org/simple/ concurrent-api-client
Requirement already satisfied: concurrent-api-client in ./venv-test/lib/python3.9/site-packages (0.0.1)
...
Requirement already satisfied: idna==3.3 in ./venv-test/lib/python3.9/site-packages (from concurrent-api-client) (3.3)
Installing collected packages: install
Successfully installed install-1.3.4

Trouble shooting

1. twine failed by HTTPError: 403 Forbidden

While I could upload to test.pypi.org, failed to upload to pypi.org by 403 Forbidden error.

1
2
3
4
$ twine upload --repository testpypi dist/*
...
HTTPError: 403 Forbidden from https://upload.pypi.org/legacy/
The user 'tatoflam' isn't allowed to upload to project 'python_api_client'. See https://pypi.org/help/#project-name for more information.

The cause is python_api_client is not reserved in test.pypi.org but in pypi.org.

Tip
It’s better to search https://pypi.org and https://test.pypi.org before deciding package name.

I changed the package name to concurrent-api-client that is not reserved. (Some document project name is replaced accordingly)

2. package dependency version unmatched

When installing the package, sometime it fails by

1
2
3
4
5
6
$ pip --no-cache-dir install --upgrade --index-url https://test.pypi.org/simple/  concurrent-api-client
Looking in indexes: https://test.pypi.org/simple/
^[[DCollecting concurrent-api-client
...
ERROR: Could not find a version that satisfies the requirement pyparsing==3.0.1 (from concurrent-api-client) (from versions: 2.2.0, 2.4.4, 2.4.6, 3.0.0a1)
ERROR: No matching distribution found for pyparsing==3.0.1

I check the versions in production pypi.org and specifies the ver. number in requirements.txt.


Sources

See finalized codes(tox.ini and setup.py) in github


References