|
| 1 | +# Publishing to PyPI |
| 2 | + |
| 3 | +This guide explains how to publish the `translateplus` package to PyPI. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. **PyPI Account**: Create an account at [pypi.org](https://pypi.org) if you don't have one |
| 8 | +2. **API Token**: Generate a PyPI API token with upload permissions |
| 9 | + |
| 10 | +## Step 1: Register the Project on PyPI (First Time Only) |
| 11 | + |
| 12 | +If this is the first time publishing, you need to register the project name on PyPI: |
| 13 | + |
| 14 | +### Option A: Using Web Interface |
| 15 | + |
| 16 | +1. Go to [pypi.org](https://pypi.org) |
| 17 | +2. Log in to your account |
| 18 | +3. Go to "Your projects" → "Add new project" |
| 19 | +4. Enter project name: `translateplus` |
| 20 | +5. Fill in the project details |
| 21 | +6. Click "Create" |
| 22 | + |
| 23 | +### Option B: Using Twine (Recommended) |
| 24 | + |
| 25 | +```bash |
| 26 | +# Install twine if not already installed |
| 27 | +pip install twine build |
| 28 | + |
| 29 | +# Build the package |
| 30 | +python -m build |
| 31 | + |
| 32 | +# Register the project (first time only) |
| 33 | +twine register dist/translateplus-2.0.0.tar.gz |
| 34 | +``` |
| 35 | + |
| 36 | +**Note**: PyPI no longer requires explicit registration. You can skip this step and go directly to uploading. |
| 37 | + |
| 38 | +## Step 2: Create PyPI API Token |
| 39 | + |
| 40 | +1. Go to [pypi.org/manage/account/](https://pypi.org/manage/account/) |
| 41 | +2. Scroll to "API tokens" |
| 42 | +3. Click "Add API token" |
| 43 | +4. Choose scope: |
| 44 | + - **For new projects**: Select "Entire account" (recommended for first upload) |
| 45 | + - **For existing projects**: Select "Project: translateplus" |
| 46 | +5. Copy the token (it starts with `pypi-`) |
| 47 | + |
| 48 | +## Step 3: Configure Authentication |
| 49 | + |
| 50 | +### Option A: Using .pypirc (Recommended) |
| 51 | + |
| 52 | +Create or edit `~/.pypirc`: |
| 53 | + |
| 54 | +```ini |
| 55 | +[pypi] |
| 56 | +username = __token__ |
| 57 | +password = pypi-YourAPITokenHere |
| 58 | +``` |
| 59 | + |
| 60 | +### Option B: Using Environment Variable |
| 61 | + |
| 62 | +```bash |
| 63 | +export TWINE_USERNAME=__token__ |
| 64 | +export TWINE_PASSWORD=pypi-YourAPITokenHere |
| 65 | +``` |
| 66 | + |
| 67 | +### Option C: Pass Directly to Twine |
| 68 | + |
| 69 | +```bash |
| 70 | +twine upload dist/* -u __token__ -p pypi-YourAPITokenHere |
| 71 | +``` |
| 72 | + |
| 73 | +## Step 4: Build the Package |
| 74 | + |
| 75 | +```bash |
| 76 | +# Install build tools |
| 77 | +pip install build twine |
| 78 | + |
| 79 | +# Clean previous builds |
| 80 | +rm -rf dist/ build/ *.egg-info |
| 81 | + |
| 82 | +# Build source distribution and wheel |
| 83 | +python -m build |
| 84 | +``` |
| 85 | + |
| 86 | +This creates: |
| 87 | +- `dist/translateplus-2.0.0.tar.gz` (source distribution) |
| 88 | +- `dist/translateplus-2.0.0-py3-none-any.whl` (wheel) |
| 89 | + |
| 90 | +## Step 5: Check the Package (Optional but Recommended) |
| 91 | + |
| 92 | +```bash |
| 93 | +# Check the package for common issues |
| 94 | +twine check dist/* |
| 95 | +``` |
| 96 | + |
| 97 | +## Step 6: Upload to PyPI |
| 98 | + |
| 99 | +### Test PyPI (Recommended for First Upload) |
| 100 | + |
| 101 | +```bash |
| 102 | +# Upload to Test PyPI first |
| 103 | +twine upload --repository testpypi dist/* |
| 104 | +``` |
| 105 | + |
| 106 | +Then test installation: |
| 107 | +```bash |
| 108 | +pip install --index-url https://test.pypi.org/simple/ translateplus |
| 109 | +``` |
| 110 | + |
| 111 | +### Production PyPI |
| 112 | + |
| 113 | +```bash |
| 114 | +# Upload to production PyPI |
| 115 | +twine upload dist/* |
| 116 | +``` |
| 117 | + |
| 118 | +Or if using environment variables: |
| 119 | +```bash |
| 120 | +twine upload dist/* --username __token__ --password $TWINE_PASSWORD |
| 121 | +``` |
| 122 | + |
| 123 | +## Troubleshooting |
| 124 | + |
| 125 | +### Error: "Invalid API Token: OIDC scoped token is not valid for project" |
| 126 | + |
| 127 | +**Solution**: This means the project doesn't exist on PyPI yet or the token is scoped incorrectly. |
| 128 | + |
| 129 | +1. **If project doesn't exist**: The first upload will automatically create it. Make sure your token has "Entire account" scope, not project-specific. |
| 130 | + |
| 131 | +2. **If using project-scoped token**: |
| 132 | + - The project name must match exactly (case-insensitive) |
| 133 | + - Try using an "Entire account" token instead |
| 134 | + |
| 135 | +3. **Check token scope**: |
| 136 | + - Go to [pypi.org/manage/account/](https://pypi.org/manage/account/) |
| 137 | + - Verify your token has the correct scope |
| 138 | + |
| 139 | +### Error: "403 Forbidden" |
| 140 | + |
| 141 | +**Possible causes**: |
| 142 | +- Invalid or expired API token |
| 143 | +- Token doesn't have upload permissions |
| 144 | +- Project name mismatch |
| 145 | + |
| 146 | +**Solution**: |
| 147 | +- Generate a new API token with "Entire account" scope |
| 148 | +- Verify the token is correct (starts with `pypi-`) |
| 149 | +- Check that you're using `__token__` as username |
| 150 | + |
| 151 | +### Error: "File already exists" |
| 152 | + |
| 153 | +**Solution**: The version already exists on PyPI. You need to: |
| 154 | +- Increment the version in `pyproject.toml` and `setup.py` |
| 155 | +- Rebuild and upload |
| 156 | + |
| 157 | +## Automated Publishing with GitHub Actions |
| 158 | + |
| 159 | +The repository includes a GitHub Actions workflow (`.github/workflows/publish.yml`) that automatically publishes when you create a release. |
| 160 | + |
| 161 | +### Setup |
| 162 | + |
| 163 | +1. Go to PyPI → Account Settings → API tokens |
| 164 | +2. Create a token with "Entire account" scope |
| 165 | +3. Go to GitHub → Repository → Settings → Secrets and variables → Actions |
| 166 | +4. Add a new secret: |
| 167 | + - Name: `PYPI_API_TOKEN` |
| 168 | + - Value: Your PyPI API token (starts with `pypi-`) |
| 169 | + |
| 170 | +### Usage |
| 171 | + |
| 172 | +1. Update version in `pyproject.toml` and `setup.py` |
| 173 | +2. Commit and push changes |
| 174 | +3. Create a new GitHub release |
| 175 | +4. The workflow will automatically build and publish to PyPI |
| 176 | + |
| 177 | +## Version Management |
| 178 | + |
| 179 | +Always update the version in both files: |
| 180 | +- `pyproject.toml`: `version = "2.0.0"` |
| 181 | +- `setup.py`: `version="2.0.0"` |
| 182 | +- `translateplus/__init__.py`: `__version__ = "2.0.0"` |
| 183 | + |
| 184 | +Follow [Semantic Versioning](https://semver.org/): |
| 185 | +- **MAJOR** (2.0.0): Breaking changes |
| 186 | +- **MINOR** (2.1.0): New features, backward compatible |
| 187 | +- **PATCH** (2.0.1): Bug fixes, backward compatible |
| 188 | + |
| 189 | +## Verification |
| 190 | + |
| 191 | +After publishing, verify the package: |
| 192 | + |
| 193 | +```bash |
| 194 | +# Install from PyPI |
| 195 | +pip install translateplus |
| 196 | + |
| 197 | +# Verify version |
| 198 | +python -c "import translateplus; print(translateplus.__version__)" |
| 199 | + |
| 200 | +# Test import |
| 201 | +python -c "from translateplus import TranslatePlusClient; print('OK')" |
| 202 | +``` |
| 203 | + |
| 204 | +Check on PyPI: https://pypi.org/project/translateplus/ |
0 commit comments