0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[python / pypi] pypi パッケージアップロード時の 403 エラー with 2FA

Last updated at Posted at 2025-04-29

概要

pypi に python の自作パッケージをアップロードしようとしたところ、 403 エラーが発生した。
username も password もログイン時に使用するものを入力できていて、間違いはない。

そんな時の対策記事です。

以下の記事の対策方法では解決しなかったので、記事化しました。

要因

pypi ログイン時に2要素認証 (2FA: 2-factor-authentication) を利用していると、この問題が発生するようです。

エラー発生状況

$ twine upload -r testpypi dist/*
Uploading distributions to https://test.pypi.org/legacy/
Uploading parapara_anime-0.0.2-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.1/12.1 kB • 00:00 • ?
WARNING  Error during upload. Retry with the --verbose option for more       
         details.                                                            
ERROR    HTTPError: 403 Forbidden from https://test.pypi.org/legacy/         
         Forbidden

対策

Pypi のサイトで API token を作成する

  • 「自分の名前」→「Account settings」→の順にクリック
    スクリーンショット 2025-04-29 20.37.01.png
  • API token を作成してください
    スクリーンショット 2025-04-29 20.37.25.png

※作成した API token は、画面を切り替えるともう取得できなくなってしまうので、作成した直後にどこかに記録してください

~/.pypirc を書き換え

~/.pypirc に記載していた username, password を以下のように修正します。

  • まだ作成していない人はファイルごと作成してください。
~/.pypirc
[distutils]
index-servers =
  pypi
  testpypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: __token__
password: pypi-<あなたの token>

[testpypi]
repository: https://test.pypi.org/legacy/
username: __token__
password: pypi-<あなたの token>

これで 403 エラーは解消できるはずです。

関連記事

公式ドキュメント - API token の作り方と使い方

この記事に記載してある問題の具体的な対処方は、以下の記事を参考にしました。

旧来の upload 方法

2段階認証を利用していない場合は以下の方法でも対応可能なはず...。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?