1. 程式人生 > >Python程式設計:twine模組打包python專案上傳pypi

Python程式設計:twine模組打包python專案上傳pypi

註冊賬號(重要)

可以配置到$HOME/.pypirc檔案中,就不用多次輸入了

[pypi]
username = <username>
password = <password>

建立專案

建立一個名為 example_pkg 的專案,目錄結構如下

example_pkg
  /example_pkg
    __init__.py

編輯檔案 example_pkg/__init__.py

name = "example_pkg"

建立包檔案

/example_pkg
  /example_pkg
    __init__.py
setup.py LICENSE README.md

建立 setup.py

按照自己的資訊,逐項填寫即可

import setuptools
import os
import requests

# 將markdown格式轉換為rst格式
def md_to_rst(from_file, to_file):
    r = requests.post(url='http://c.docverter.com/convert',
                      data={'to':'rst','from':'markdown'},
                      files={'input_files[]'
:open(from_file,'rb')}) if r.ok: with open(to_file, "wb") as f: f.write(r.content) md_to_rst("README.md", "README.rst") long_description = 'Add a fallback short description here' if os.path.exists('README.rst'): long_description = open('README.rst', encoding="utf-8").read() setuptools.setup( name="chinesename"
, version="0.0.8", author="Peng Shiyu", license = 'MIT License', author_email="[email protected]", description="get a chinesename by random", long_description=long_description, long_description_content_type="text/x-rst", url="https://github.com/mouday/chinesename", packages=setuptools.find_packages(), classifiers=( "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ), install_requires = [], # 常用 # include_package_data=True, # 自動打包資料夾內所有資料 package_data = { # If any package contains *.txt or *.rst files, include them: 'chinesename': ['source/*.txt', "source/*.json"], } )

建立 README.md

建議寫的詳細些,展示你專案的主要介紹

 # Example Package

This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.

官網上說支援markdown格式,可是顯示不正確,可以將.md檔案轉為.rst檔案, 也有推薦說使用 Pandoc裝換,我沒成功,所以使用了setup.py中的方法

生成目錄樹,新增檔案目錄說明:

tree /F > tree.txt

建立 LICENSE

可以忽略

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.

生成釋出壓縮包

確保已經安裝setuptoolswheel

python3 -m pip install --user --upgrade setuptools wheel

setup.py檔案同目錄命令列下執行

python3 setup.py sdist bdist_wheel

產生兩個檔案

dist/
  example_pkg-0.0.1-py3-none-any.whl
  example_pkg-0.0.1.tar.gz

tar.gz 原始檔
.whl 分發檔案

檢查打包的檔案是否正常

python setup.py install  # 安裝

按照使用方式匯入測試,沒問題後繼續

上傳檔案

安裝twine

pip install twine

上傳

twine upload dist/*

沒有報錯就成功了

Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your username]
Enter your password:
Uploading example_pkg-0.0.1-py3-none-any.whl
100%|█████████████████████| 4.65k/4.65k [00:01<00:00, 2.88kB/s]
Uploading example_pkg-0.0.1.tar.gz
100%|█████████████████████| 4.25k/4.25k [00:01<00:00, 3.05kB/s]

安裝你剛剛上傳的包

pip install -i https://test.pypi.org/simple/ example_pkg

匯入測試

>>> import example_pkg
>>> example_pkg.name
'example_pkg'

總結

釋出專案分三步
1. 配置setup.py檔案
2. 打包專案
3. 釋出專案

python setup.py sdist bdist_wheel

twine upload dist/*