打包发布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 protected]',
         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
22
[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代码的头文件安装路径)
---------------------
作者:KurolZ
来源:CSDN
原文:https://blog.csdn.net/hjxzt1/article/details/73741495

猜你喜欢

转载自www.cnblogs.com/hellojack/p/10749885.html