0. 前言
昨天無聊寫了一個小遊戲,然後突然想到要是能使用 pip
安裝不就很棒(可以更好的跟女朋友炫耀)。然後網上找了一下資料,發現還真可以,還挺容易的。
接下來就以我寫的小遊戲爲例,做一個示範。
1. 註冊賬號
點此註冊:Register Pypi
2. 安裝 setuotools, wheel, twine
setuotools
和 wheel
用來構建你的項目,一般都會隨 Python 安裝,但是還是檢查一下:
python -m pip install --user --upgrade setuptools wheel
twine
用來上傳你的包到 PyPi :
python -m pip install --user --upgrade twine
3. 添加文件
3.1 目錄結構
我寫的小遊戲比較小巧,只有一個文件 life.py
,但是 python 包需要一個 __init__.py
文件,由於我想直接運行包,所以加了一個 __main__.py
文件,但是這個文件非必需。所以,目錄結構如下:
life_game\
|---- __init__.py
|---- __main__.py
|---- life.py
接下來添加一些發佈的必要文件,在上面的文件夾外層再創建一個文件夾,名字隨意,我習慣使用一樣的。如下:
life_game\
|---- setup.py
|---- LICENSE
|---- README.md
|---- life_game\
|---- __init__.py
|---- __main__.py
|---- life.py
3.2 setup.py
setup.py
是setuptools的構建腳本。它告訴setuptools你的包(例如名稱和版本)以及要包含的代碼文件。
我的 setup.py 如下:
#!/usr/bin/env python
from __future__ import print_function
from setuptools import setup, find_packages
import sys
setup(
name="life_game",
version="1.2.0",
author="Memory",
author_email="[email protected]",
description="life game",
long_description="life game written by pygame",
license="MIT",
url="https://github.com/MemoryD/life_game",
packages=setuptools.find_packages(),
install_requires=[
"pygame <= 1.9.5",
],
classifiers=[
"Topic :: Games/Entertainment ",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
],
)
name
是包名。發佈之前請上 PyPi 搜索一下有沒有同名的包,防止衝突。version
是版本號,更新的時候會尋找比當前版本更高的版本號,所以不要亂寫。description
是短描述,一般是一句話。long_description
是長描述,詳細的介紹。但是我比較懶,也只寫了一句話。url
是你項目的地址。一般會填github
地址。packages
是包列表。setuptools.find_packages()
可以自動找到目錄下所有的包,在這個例子中是 “life_game”, 只有一個包。install_requires
是這個包的所需的依賴。classifiers
是分類。根據 PyPi Classifiers 填寫,至少要包含所用的 Python 版本。
3.3 LICENSE
上傳到 PyPi 的每個包都要包含許可證。
選擇許可證:Choose License。
選擇許可證後,打開 LICENSE
並輸入許可證文本。選擇了 MIT 許可證:
Copyright (c) 2018 The Python Packaging Authority
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4. 構建項目
以下的命令 ,都在在外層 life_game
,也就是 setup.py
所在的目錄進行。
-
檢查一下
setup.py
文件是否有錯誤:python setup.py check
沒有錯誤才能進行構建。
-
構建:
python setup.py sdist bdist_wheel
如果沒有錯誤,則文件夾下面會生成好幾個目錄,其中重要的是 dist
目錄:
dist\
|---- life_game-1.2.0.tar.gz
|---- life_game-1.2.0-py3-none-any.whl
這兩個文件是要上傳到 PyPi 網站上供別人下載安裝的。
5. 上傳
5.1 方法一
twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
然後輸入你在 PyPi 註冊的賬號和密碼就可以上傳了。如下:
> twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: [username]
Enter your password: [password]
Uploading life_game-1.2.0-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████| 7.41k/7.41k [00:02<00:00, 3.19kB/s]
Uploading life_game-1.2.0.tar.gz
100%|█████████████████████████████████████████████████████████████████████████████| 7.12k/7.12k [00:00<00:00, 11.7kB/s]
5.2 方法二
方法一有時候這樣子上傳好像會失敗,而且每次都要輸入 用戶名和密碼。
這時候我們就可以創建一個配置文件,簡化每次的上傳流程。
創建一個文件 .pypric
-
window 下,將
.pypric
放在C:\\User\用戶名\
下。如果不能直接創建,就用cmd
輸入命令:echo a 2>.pypric
-
linux 下,將
.pypric
放在~/
也就是%HOME
目錄下。
創建好後,編輯,輸入以下內容保存:
[distutils]
index-servers=pypi
[pypi]
repository = https://upload.pypi.org/legacy/
username: [username]
password: [password]
這時候要上傳項目只需:
twine upload dist/*
然後就可以使用 pip 安裝你的包了。
5. 小結
感覺發布在 PyPi 上以後就變得高大上了(雖然自己還是很菜),不過看着別人(女朋友)使用 pip 直接安裝自己的項目還是挺有成就感的。
有趣的的可以試一下這個 小遊戲。經典的生命遊戲。
安裝
pip install life_game
運行
python -m life_game
如果看完了發現還有疑問的話, 可以查閱官方文檔:Packaging Python Projects