|
| 1 | +# PyPI 发布指南 |
| 2 | + |
| 3 | +本文档说明如何将 hyperliquid-mcp-python 发布到 PyPI。 |
| 4 | + |
| 5 | +## 前置要求 |
| 6 | + |
| 7 | +1. **注册 PyPI 账号** |
| 8 | + - 主站:https://pypi.org/account/register/ |
| 9 | + - 测试站(建议先用这个):https://test.pypi.org/account/register/ |
| 10 | + |
| 11 | +2. **生成 API Token** |
| 12 | + - 主站:https://pypi.org/manage/account/token/ |
| 13 | + - 测试站:https://test.pypi.org/manage/account/token/ |
| 14 | + - 选择 "Scope: Entire account" 或指定项目 |
| 15 | + - 保存生成的 token(格式:`pypi-...`) |
| 16 | + |
| 17 | +3. **安装发布工具** |
| 18 | + ```bash |
| 19 | + uv pip install build twine |
| 20 | + ``` |
| 21 | + |
| 22 | +## 发布步骤 |
| 23 | + |
| 24 | +### 步骤 1: 清理旧构建文件 |
| 25 | + |
| 26 | +```bash |
| 27 | +# 删除之前的构建产物 |
| 28 | +rm -rf dist/ build/ *.egg-info |
| 29 | +``` |
| 30 | + |
| 31 | +### 步骤 2: 构建分发包 |
| 32 | + |
| 33 | +```bash |
| 34 | +# 使用 uv 构建 |
| 35 | +uv build |
| 36 | + |
| 37 | +# 或使用 python -m build |
| 38 | +python -m build |
| 39 | +``` |
| 40 | + |
| 41 | +这会在 `dist/` 目录生成两个文件: |
| 42 | +- `.tar.gz` (源码分发) |
| 43 | +- `.whl` (wheel 分发) |
| 44 | + |
| 45 | +### 步骤 3: 检查构建产物 |
| 46 | + |
| 47 | +```bash |
| 48 | +# 检查包的完整性 |
| 49 | +twine check dist/* |
| 50 | +``` |
| 51 | + |
| 52 | +### 步骤 4: 先发布到测试 PyPI(推荐) |
| 53 | + |
| 54 | +```bash |
| 55 | +# 使用 token 上传到测试服务器 |
| 56 | +twine upload --repository testpypi dist/* |
| 57 | + |
| 58 | +# 会提示输入: |
| 59 | +# Username: __token__ |
| 60 | +# Password: pypi-... (粘贴你的 token) |
| 61 | +``` |
| 62 | + |
| 63 | +### 步骤 5: 从测试 PyPI 安装验证 |
| 64 | + |
| 65 | +```bash |
| 66 | +# 从测试 PyPI 安装 |
| 67 | +pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ hyperliquid-mcp-python |
| 68 | + |
| 69 | +# 测试安装的包 |
| 70 | +hyperliquid-mcp --help |
| 71 | +``` |
| 72 | + |
| 73 | +### 步骤 6: 发布到正式 PyPI |
| 74 | + |
| 75 | +如果测试没问题,发布到正式 PyPI: |
| 76 | + |
| 77 | +```bash |
| 78 | +twine upload dist/* |
| 79 | + |
| 80 | +# 会提示输入: |
| 81 | +# Username: __token__ |
| 82 | +# Password: pypi-... (粘贴你的正式 PyPI token) |
| 83 | +``` |
| 84 | + |
| 85 | +### 步骤 7: 验证安装 |
| 86 | + |
| 87 | +```bash |
| 88 | +# 从正式 PyPI 安装 |
| 89 | +pip install hyperliquid-mcp-python |
| 90 | + |
| 91 | +# 或使用 uv |
| 92 | +uv pip install hyperliquid-mcp-python |
| 93 | +``` |
| 94 | + |
| 95 | +## 使用 ~/.pypirc 简化流程(可选) |
| 96 | + |
| 97 | +创建 `~/.pypirc` 文件保存 token: |
| 98 | + |
| 99 | +```ini |
| 100 | +[distutils] |
| 101 | +index-servers = |
| 102 | + pypi |
| 103 | + testpypi |
| 104 | + |
| 105 | +[pypi] |
| 106 | +username = __token__ |
| 107 | +password = pypi-你的正式token |
| 108 | + |
| 109 | +[testpypi] |
| 110 | +repository = https://test.pypi.org/legacy/ |
| 111 | +username = __token__ |
| 112 | +password = pypi-你的测试token |
| 113 | +``` |
| 114 | + |
| 115 | +设置文件权限: |
| 116 | +```bash |
| 117 | +chmod 600 ~/.pypirc |
| 118 | +``` |
| 119 | + |
| 120 | +有了这个配置后,上传时不需要输入密码: |
| 121 | +```bash |
| 122 | +# 测试服务器 |
| 123 | +twine upload -r testpypi dist/* |
| 124 | + |
| 125 | +# 正式服务器 |
| 126 | +twine upload dist/* |
| 127 | +``` |
| 128 | + |
| 129 | +## 发布新版本 |
| 130 | + |
| 131 | +1. 更新版本号(`pyproject.toml` 中的 `version`) |
| 132 | +2. 更新 README 或 CHANGELOG |
| 133 | +3. 提交并打 tag: |
| 134 | + ```bash |
| 135 | + git add -A |
| 136 | + git commit -m "chore: bump version to 0.1.1" |
| 137 | + git tag v0.1.1 |
| 138 | + git push origin main --tags |
| 139 | + ``` |
| 140 | +4. 清理并重新构建: |
| 141 | + ```bash |
| 142 | + rm -rf dist/ |
| 143 | + uv build |
| 144 | + ``` |
| 145 | +5. 上传新版本: |
| 146 | + ```bash |
| 147 | + twine upload dist/* |
| 148 | + ``` |
| 149 | + |
| 150 | +## 自动化发布(GitHub Actions) |
| 151 | + |
| 152 | +可以创建 `.github/workflows/publish.yml` 实现自动发布: |
| 153 | + |
| 154 | +```yaml |
| 155 | +name: Publish to PyPI |
| 156 | + |
| 157 | +on: |
| 158 | + release: |
| 159 | + types: [published] |
| 160 | + |
| 161 | +jobs: |
| 162 | + publish: |
| 163 | + runs-on: ubuntu-latest |
| 164 | + steps: |
| 165 | + - uses: actions/checkout@v4 |
| 166 | + - uses: actions/setup-python@v5 |
| 167 | + with: |
| 168 | + python-version: '3.11' |
| 169 | + - name: Install dependencies |
| 170 | + run: | |
| 171 | + pip install build twine |
| 172 | + - name: Build package |
| 173 | + run: python -m build |
| 174 | + - name: Publish to PyPI |
| 175 | + env: |
| 176 | + TWINE_USERNAME: __token__ |
| 177 | + TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} |
| 178 | + run: twine upload dist/* |
| 179 | +``` |
| 180 | +
|
| 181 | +需要在 GitHub 仓库设置中添加 Secret: `PYPI_API_TOKEN` |
| 182 | + |
| 183 | +## 常见问题 |
| 184 | + |
| 185 | +### 错误:文件已存在 |
| 186 | +如果上传时提示文件已存在,说明该版本号已发布过。需要: |
| 187 | +1. 更新版本号 |
| 188 | +2. 重新构建 |
| 189 | +3. 再次上传 |
| 190 | + |
| 191 | +### 错误:包名已被占用 |
| 192 | +如果包名被占用,需要修改 `pyproject.toml` 中的 `name` 字段。 |
| 193 | + |
| 194 | +### 包大小问题 |
| 195 | +确保 `.gitignore` 中排除了不需要的文件: |
| 196 | +- `__pycache__/` |
| 197 | +- `*.pyc` |
| 198 | +- `.env` |
| 199 | +- `hyperliquid_mcp.log` |
| 200 | + |
| 201 | +## 验证清单 |
| 202 | + |
| 203 | +发布前检查: |
| 204 | +- [ ] LICENSE 文件存在 |
| 205 | +- [ ] README.md 完整且格式正确 |
| 206 | +- [ ] version 号正确 |
| 207 | +- [ ] dependencies 版本号合理 |
| 208 | +- [ ] 在测试 PyPI 上验证通过 |
| 209 | +- [ ] 本地可以正常 import 和运行 |
| 210 | + |
| 211 | +## 参考资源 |
| 212 | + |
| 213 | +- [Python Packaging Guide](https://packaging.python.org/) |
| 214 | +- [Twine Documentation](https://twine.readthedocs.io/) |
| 215 | +- [PyPI Help](https://pypi.org/help/) |
0 commit comments