|
| 1 | + |
| 2 | +.. _pypirc: |
| 3 | + |
| 4 | +======================== |
| 5 | +The :file:`.pypirc` file |
| 6 | +======================== |
| 7 | + |
| 8 | +A :file:`.pypirc` file allows you to define the configuration for :term:`package |
| 9 | +indexes <Package Index>` (referred to here as "repositories"), so that you don't |
| 10 | +have to enter the URL, username, or password whenever you upload a package with |
| 11 | +:ref:`twine` or :ref:`flit`. |
| 12 | + |
| 13 | +The format (originally defined by the :ref:`distutils` package) is: |
| 14 | + |
| 15 | +.. code-block:: ini |
| 16 | +
|
| 17 | + [distutils] |
| 18 | + index-servers = |
| 19 | + first-repository |
| 20 | + second-repository |
| 21 | +
|
| 22 | + [first-repository] |
| 23 | + repository = <first-repository URL> |
| 24 | + username = <first-repository username> |
| 25 | + password = <first-repository password> |
| 26 | +
|
| 27 | + [second-repository] |
| 28 | + repository = <second-repository URL> |
| 29 | + username = <second-repository username> |
| 30 | + password = <second-repository password> |
| 31 | +
|
| 32 | +The ``distutils`` section defines an ``index-servers`` field that lists the |
| 33 | +name of all sections describing a repository. |
| 34 | + |
| 35 | +Each section describing a repository defines three fields: |
| 36 | + |
| 37 | +- ``repository``: The URL of the repository. |
| 38 | +- ``username``: The registered username on the repository. |
| 39 | +- ``password``: The password that will used to authenticate the username. |
| 40 | + |
| 41 | +.. warning:: |
| 42 | + |
| 43 | + Be aware that this stores your password in plain text. For better security, |
| 44 | + consider an alternative like `keyring`_, setting environment variables, or |
| 45 | + providing the password on the command line. |
| 46 | + |
| 47 | +.. _keyring: https://pypi.org/project/keyring/ |
| 48 | + |
| 49 | +Common configurations |
| 50 | +===================== |
| 51 | + |
| 52 | +.. note:: |
| 53 | + |
| 54 | + These examples apply to :ref:`twine`, and projects like :ref:`hatch` that |
| 55 | + use it under the hood. Other projects (e.g. :ref:`flit`) also use |
| 56 | + :file:`.pypirc`, but with different defaults. Please refer to each project's |
| 57 | + documentation for more details and usage instructions. |
| 58 | + |
| 59 | +Twine's default configuration mimics a :file:`.pypirc` with repository sections |
| 60 | +for PyPI and TestPyPI: |
| 61 | + |
| 62 | +.. code-block:: ini |
| 63 | +
|
| 64 | + [distutils] |
| 65 | + index-servers = |
| 66 | + pypi |
| 67 | + testpypi |
| 68 | +
|
| 69 | + [pypi] |
| 70 | + repository = https://upload.pypi.org/legacy/ |
| 71 | +
|
| 72 | + [testpypi] |
| 73 | + repository = https://test.pypi.org/legacy/ |
| 74 | +
|
| 75 | +Twine will add additional configuration from :file:`$HOME/.pypirc`, the command |
| 76 | +line, and environment variables to this default configuration. |
| 77 | + |
| 78 | +Using a PyPI token |
| 79 | +------------------ |
| 80 | + |
| 81 | +To set your `API token`_ for PyPI, you can create a :file:`$HOME/.pypirc` |
| 82 | +similar to: |
| 83 | + |
| 84 | +.. code-block:: ini |
| 85 | +
|
| 86 | + [pypi] |
| 87 | + username = __token__ |
| 88 | + password = <PyPI token> |
| 89 | +
|
| 90 | +For :ref:`TestPyPI <using-test-pypi>`, add a ``[testpypi]`` section, using the |
| 91 | +API token from your TestPyPI account. |
| 92 | + |
| 93 | +.. _API token: https://pypi.org/help/#apitoken |
| 94 | + |
| 95 | +Using another package index |
| 96 | +--------------------------- |
| 97 | + |
| 98 | +To configure an additional repository, you'll need to redefine the |
| 99 | +``index-servers`` field to include the repository name. Here is a complete |
| 100 | +example of a :file:`$HOME/.pypirc` for PyPI, TestPyPI, and a private repository: |
| 101 | + |
| 102 | +.. code-block:: ini |
| 103 | +
|
| 104 | + [distutils] |
| 105 | + index-servers = |
| 106 | + pypi |
| 107 | + testpypi |
| 108 | + private-repository |
| 109 | +
|
| 110 | + [pypi] |
| 111 | + username = __token__ |
| 112 | + password = <PyPI token> |
| 113 | +
|
| 114 | + [testpypi] |
| 115 | + username = __token__ |
| 116 | + password = <TestPyPI token> |
| 117 | +
|
| 118 | + [private-repository] |
| 119 | + repository = <private-repository URL> |
| 120 | + username = <private-repository username> |
| 121 | + password = <private-repository password> |
| 122 | +
|
| 123 | +.. warning:: |
| 124 | + |
| 125 | + Instead of using the ``password`` field, consider saving your API tokens |
| 126 | + and passwords securely using `keyring`_ (which is installed by Twine): |
| 127 | + |
| 128 | + .. code-block:: bash |
| 129 | +
|
| 130 | + keyring set https://upload.pypi.org/legacy/ __token__ |
| 131 | + keyring set https://test.pypi.org/legacy/ __token__ |
| 132 | + keyring set <private-repository URL> <private-repository username> |
0 commit comments