Python学习笔记-打包发布Python模块或程序,安装包

简介:

Python模块、扩展和应用程序可以按以下几种形式进行打包和发布:

 

python setup.py获取帮助的方式

    python setup.py --help

    python setup.py --help-commands 所有可以使用的命令,如build,install

    python setup.py COMMAND --help 获取特定命令的帮助

    python setup.py COMMAND --help-formats 获取特定命令支持使用的格式

 

打包

    1.压缩文件(使用distutils)

        Windows的Zip文件和类Unix平台的.tar.gz文件

    2.自动解包或自动安装可执行文件

        Windows中的.exe文件

    3.自包含的,不要求安装的预备运行科执行程序

        Windows的.exe文件、Unix上带有一个小的脚本前缀的ZIP压缩文件、Mac上的.app文件等

    4.平台相关的安装程序

        Windows上的.msi文件、Linux上常见的.rpm、src.rpm和.dep文件等

    5.Python eggs

        较流行的第三方扩展

 

 

发布

    “发布”是指一个文件集合,这些文件联合在一起可使用distutils构建、打包和发布模块

    创建好的发布可以用于安装,可上传到ftp,上传到各大网络让人下载,也可上传到PyPI与他人共享

 

创建发布

    将各代码文件组织到模块容器中

    准备一个README或README.txt文件

    而后在容器中创建setup.py文件

 

setup.py中setup()中可用参数:

    platforms: 平台列表

    license: 许可证

    py_modules: 各模块名称组成的列表,此些模块可能位于包的根目录下(modname),也可能位于某子包目录中(subpkg1.modname)

    packages: 各子包名称的列表

    ......

    

setup.py关键字大体分为两类:

1.元数据信息

2.包中的内容列表

 

python setup.py sdist打包(会指定默认格式tar.gz)

    可以为sdist指定打包格式:

            zip: zip file

            gztar: tar.gz file

            bztar: tar.bz2 vil2

            ztar: tar.Z file

            tar: tar file

指定格式sdist打包的方式:

1
2
3
4
5
6
7
8
[root@kurol pkg1] # python36 setup.py sdist --help-formats
List  of available source distribution formats:
   - - formats = bztar  bzip2'ed tar - file
   - - formats = gztar  gzip'ed tar - file
   - - formats = tar    uncompressed tar  file
   - - formats = xztar  xz'ed tar - file
   - - formats = zip     ZIP  file
   - - formats = ztar   compressed tar  file


python setup.py bdist打包(二进制发行版)(会指定默认格式tar.gz)

        可以为bdist指定的格式:

                gztar: tar.gz file

                ztar: tar.Z file

                zip: zip file

                rpm: RPM Package

                pkgtool: Solaris pkgtool

                wininst: Windows上自解压的zip格式包

                msi:Microsoft Installer

指定格式sdist打包的方式:

1
2
3
4
5
6
7
8
9
10
11
[root@kurol pkg1] # python36 setup.py bdist --help-formats 
List  of available distribution formats:
   - - formats = rpm      RPM distribution
   - - formats = gztar    gzip'ed tar  file
   - - formats = bztar    bzip2'ed tar  file
   - - formats = xztar    xz'ed tar  file
   - - formats = ztar     compressed tar  file
   - - formats = tar      tar  file
   - - formats = wininst  Windows executable installer
   - - formats = zip       ZIP  file
   - - formats = msi      Microsoft Installer


打包 ,例:

1
2
3
4
5
6
[root@kurol python361] # cd pkg1/
[root@kurol pkg1] # ls
__init__.py __pycache__ mymmm.py
[root@kurol pkg1] # touch REMAIN.txt
[root@kurol pkg1] # touch setup.py
[root@kurol pkg1] # vim setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #!/usr/bin/python36
  #
  from  distutils.core  import  setup
  
  setup(
          name  =  'pkg1' ,
          version  =  '1.0' ,
          author  =  'MageEdu' ,
          author_email  =  'email@mykurol.com' ,
          py_modules  =  [ 'mymmm' ],
          url  =  'http://www.mykurol.com' ,
          download_url  =  'http://www.mykurol.com/pymodules/download/' ,
          description  =  'test module' ,
  )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@kurol pkg1] # python36 setup.py sdist
running sdist
running check
warning: sdist: manifest template  'MANIFEST.in'  does not exist (using default  file  list)
 
warning: sdist: standard  file  not found: should have one of README, README.txt
 
file  yammm.py ( for  module yammm) not found
writing manifest  file  'MANIFEST'
creating pkg1-1.0
making hard links  in  pkg1-1.0...
hard linking setup.py -> pkg1-1.0
creating dist
Creating  tar  archive
removing  'pkg1-1.0'  (and everything under it)
 
[root@kurol pkg1] # ls    ##自动生成了MANIFEST文件
MANIFEST  REMAIN.txt  __init__.py  __pycache__  dist  mymmm.py  setup.py
[root@kurol pkg1] # cd dist/
[root@kurol dist] # ls
<strong>pkg1-1.0. tar .gz< /strong >


使用bdist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@kurol dist] # cd -
/opt/python361/pkg1
[root@kurol pkg1] # python36 setup.py bdist 
running bdist
running bdist_dumb
running build
running build_py
file  yammm.py ( for  module yammm) not found
file  yammm.py ( for  module yammm) not found
installing to build /bdist .linux-x86_64 /dumb
running  install
running install_lib
warning: install_lib:  'build/lib'  does not exist -- no Python modules to  install
 
running install_egg_info
Creating build /bdist .linux-x86_64 /dumb/usr/local/python361/lib/python3 .6 /site-packages/
Writing build /bdist .linux-x86_64 /dumb/usr/local/python361/lib/python3 .6 /site-packages/pkg1-1 .0-py3.6.egg-info
Creating  tar  archive
removing  'build/bdist.linux-x86_64/dumb'  (and everything under it)
[root@kurol pkg1] # cd dist/
[root@kurol dist] # ls
<strong>pkg1-1.0.linux-x86_64. tar .gz< /strong >  pkg1-1.0. tar .gz


指定为zip格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@kurol pkg1] # python36 setup.py bdist --formats=zip
running bdist
running bdist_dumb
running build
running build_py
file  yammm.py ( for  module yammm) not found
file  yammm.py ( for  module yammm) not found
installing to build /bdist .linux-x86_64 /dumb
running  install
running install_lib
warning: install_lib:  'build/lib'  does not exist -- no Python modules to  install
 
running install_egg_info
Creating build /bdist .linux-x86_64 /dumb/usr/local/python361/lib/python3 .6 /site-packages/
Writing build /bdist .linux-x86_64 /dumb/usr/local/python361/lib/python3 .6 /site-packages/pkg1-1 .0-py3.6.egg-info
creating  '/opt/python361/pkg1/dist/pkg1-1.0.linux-x86_64.zip'  and adding  '.'  to it
adding  'usr/local/python361/lib/python3.6/site-packages/pkg1-1.0-py3.6.egg-info'
removing  'build/bdist.linux-x86_64/dumb'  (and everything under it)
[root@kurol pkg1] # cd dist/
[root@kurol dist] # ls
pkg1-1.0.linux-x86_64. tar .gz  <strong>pkg1-1.0.linux-x86_64.zip< /strong >  pkg1-1.0. tar .gz


安装包

    python setup.py install

    build and install:

    python setup.py build:

        --build-base /path/to/build_dir      ##指定build路径,build在其他路径进行,保证源码的整洁程度

            lib,lib.platform

第三方模块的大多数默认路径通常为:site-packages(站点包)

如 /usr/local/python361/lib/python3.6/site-packages

 

第三方模块自定义安装路径:

    --user 如果用户没有写权限,安装到指定用户的目录下(只有普通权限,没有办法写到公共目录中)

    --prefix 指定python库文件的安装路径(对公共目录有写权限才能操作)

    --exec-prefix 跟python无关的,有其他语言所实现的跟平台有关的,已经编译好的相关文件的安装路径(对公共目录有写权限才能操作)

 

深度定制 (期望对python模块安装做深度定制)(路径都是自己定义):

    --install-purelib /path/to/python_lib    (纯Python库文件)

    --install-platlib /paht/to/plat_lib    (扩展模块,其他语言所实现的)

       --install-lib /path/to/lib ( 也可不加区分)

        如果同时出现前面3种,第三种lib覆盖前面2种,lib优先级最高。

    --install-scripts /path/to/bin(可执行文件的安装路径)

    --install-data (指定数据文件安装路径)

    --install-headers(指定C代码的头文件安装路径)





      本文转自谢育政 51CTO博客,原文链接:http://blog.51cto.com/kurolz/1935033,如需转载请自行联系原作者





相关文章
|
9月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
1033 7
|
9月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
793 0
|
9月前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
1573 68
|
9月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
738 4
|
9月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
813 0
|
9月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1273 0
|
10月前
|
设计模式 决策智能 Python
Python条件控制:让程序学会"思考"的魔法
本文深入浅出地讲解Python条件控制,从基础if语句到多分支、嵌套结构,再到简洁的三元表达式与Python 3.10新增的match-case模式匹配,结合电商折扣、会员等级、ATM系统等实战案例,全面掌握程序“智能决策”的核心逻辑。
627 0
|
10月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1667 102

推荐镜像

更多