1. 程式人生 > >【】建立自己的python模組,上傳到pypi上

【】建立自己的python模組,上傳到pypi上

零、先祭出官網連線

一、建立檔案的目錄結構

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

__init__.py

初始化的資料,之後pip install調包的時候會先載入這裡面的資料,所以裡面還需要加上

from . import string2date

寫的方法

from setuptools import setup

setup(name='string2date',
      version='0.0.1',
      description=
'string to date', url='https://github.com/BigDataFounder/string2date', author='hurpe', author_email='[email protected]', license='MIT', packages=['string2date'])
  • name
    • 就是包名
  • version
    • 包的版本號
  • packages
    • 可引入的包的名字,也可以使用setuptools.find_packages()去自動匯入,我這裡就一個包,所以直接寫了

自定義的一項,可以簡單描述一下包是做什麼的

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.

二、生成檔案

如果上述工作都做完了,那麼恭喜你可以開始準備上傳包了,之後就可以用pip install下載工具了。

setuptools 和wheel

首先需要保證你有最新版的setuptoolswheel

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

進入到和setup.py同級目錄下

python setup.py sdist bdist_wheel

這時會生成兩個檔案

dist/
  string2date-0.0.1-py2-none-any.whl
  string2date-0.0.1.tar.gz

三、上傳檔案到Pypi

利用twine將包上傳上去

首先先安裝twine

python -m pip install --user --upgrade twine

註冊一個Pypi的賬號

之後會用到帳號密碼

Tip

  • 不用翻牆
  • 有個郵箱驗證,需要登入郵箱驗證一下

上傳

twine upload dist/*
  • 預設直接上傳到了pypi上,如果想指定上傳的位置,也可以指定,如下是上傳到測試的
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

注意:這地方我遇到了個坑,可能因為我這個電腦python裝了好幾個版本,還有anaconda,所以我在下載twine的時候不在我配置環境變數的路徑裡,上傳的命令一直不能執行,找了半天又加了一個twine的環境變數才能用

#### 上傳成功的命令列

G:\pycode\hurpe\string2date>twine upload dist/string2date-0.0.1*
Enter your username: hurpe
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading string2date-0.0.3-py2-none-any.whl
100%|█████████████████████████████████████| 5.28k/5.28k [00:05<00:00, 1.02kB/s]
Uploading string2date-0.0.3.tar.gz
100%|█████████████████████████████████████| 3.85k/3.85k [00:01<00:00, 2.89kB/s]

四、登入Pypi檢視

在這裡插入圖片描述

五、檢驗

這時候就可以,下載包,然後執行裡面方法了

pip install string2date
G:\pycode\hurpe\string2date>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import string2date
>>>

六、更新版本

  • 更新版本也很簡單,只需要修改setup.py下的version
  • 然後重新生成檔案,上傳
python setup.py sdist bdist_wheel
twine upload dist/string2date-0.0.2*

七、更新本地moudle版本

pip install --upgrade string2date

或者是先解除安裝,再安裝

# 解除安裝string2date
pip uninstall string2date
# 安裝string2date
pip install string2date