Python3 入門6 庫的打包與安裝

2021-08-09 03:49:29 字數 2108 閱讀 3332

原文:

庫發布前先確認你的庫是以下這個結構的. 

project 是最外層的庫名 

package1 是裡面的包名 

module.py 是具體的模組. 

setup.py__init__.py是必須包含的,下面會講解

project

┝━ setup.py

┝━ package1

│ ┝ __init__.py

│ ┝ module1.py

│ └ module2.py

└─ package2

┝ __init__.py

┝ module3.py

└ module4.py

下面是我的包結構示範圖: 

以下為示範,簡單寫了個測試函式

#coding=utf-8

# module.py 常用模組

def hello():

print("hello world!")

if __name__ == "__main__":

hello()

寫入以下內容, 具體含義以註解,根據需求修改

#coding=utf-8

from distutils.core import setup

setup(name="python", version="1.0", description="my python", url='', author="luzhuo")

寫入以下內容,具體含義以註解,根據需求修改 

可以把所有的模組名都寫上

#coding=utf-8

# from mypthon import * 時匯入的模組名

__all__=['module']

cd到project目錄執行(有setup.py檔案的目錄), 執行 構建命令

python setup.py build
構建完, 執行 打包命令

python setup.py sdist
如果拿到的是 ***.tar.gz 的壓縮包,要先完全解壓

tar -zxvf ***.tar.gz
完全解壓完之後,cd到解壓出來的庫根目錄(有setup.py檔案的目錄), 執行 安裝命令 

windows:

python setup.py install
linux:

sudo python setup.py install --record log
安裝完後即可執行呼叫命令

>>> from moltest import *

>>> module.hello()

hello world!

>>>

windows:

也可搜尋site-packages資料夾.

linux:

解除安裝 

已安裝的庫 

Python3入門與高階

python3入門與高階 第1章 python入門導學 第1章 python入門導學.mp4 第2章 python環境安裝 第2章 python環境安裝.mp4 第3章 理解什麼是寫 與python的基本型別 第3章 理解什麼是寫 與python的基本型別.mp4 第4章 python中表示 組 的...

Python3離線打包安裝

使用conda constructor conda install constructor constructor construct.yaml name chq grid decode version 0.1 install in dependency order true ignore dupl...

Python3 檔案打包與解包

python 中內建了打包的庫,我們直接使用即可 zip usr bin env python3 coding utf 8 import zipfile zip 打包 z zipfile.zipfile black.zip w 建立乙個空包 zip 解包 z zipfile.zipfile blac...