python 打包与部署

时间:2021-11-12 14:30:04

环境:win10、eclipse-jee-mars、python2.7

打包在linux上进行安装测试

1.1 打包

项目目录结构如下:

python 打包与部署

打包对象:utils.reg

在P1项目的*目录,也就是utils包的父目录下创建setup.py文件,内容如下:

#coding=utf-8

from distutils.core import setup

setup(

name='reg',

version='v1.0',

py_modules=['utils.reg']

)

直接运行该文件会报以下错误:

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]

or: setup.py --help [cmd1 cmd2 ...]

or: setup.py --help-commands

or: setup.py cmd --help

error: no commands supplied

编译

python setup.py build

python 打包与部署

编译后在setup.py同目录下多出一个build目录

python 打包与部署

打包

G:\SVN\workspaces\P1\src>python setup.py sdist

running sdist

running check

warning: check: missing required meta-data: url

warning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be supplied

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

writing manifest file 'MANIFEST'

creating reg-v1.0

creating reg-v1.0\utils

copying files to reg-v1.0...

copying setup.py -> reg-v1.0

copying utils\__init__.py -> reg-v1.0\utils

copying utils\reg.py -> reg-v1.0\utils

creating dist

creating 'dist\reg-v1.0.zip' and adding 'reg-v1.0' to it

adding 'reg-v1.0\PKG-INFO'

adding 'reg-v1.0\setup.py'

adding 'reg-v1.0\utils\reg.py'

adding 'reg-v1.0\utils\__init__.py'

removing 'reg-v1.0' (and everything under it)

打包后会在setup.py同目录下多出一个disk目录,存放打好的包

python 打包与部署

1.2 部署安装

python setup.py install

[root@bak 201601]# unzip reg-v1.0.zip

Archive: reg-v1.0.zip

inflating: reg-v1.0/PKG-INFO

inflating: reg-v1.0/setup.py

inflating: reg-v1.0/utils/reg.py

inflating: reg-v1.0/utils/__init__.py

[root@bak 201601]#

[root@bak 201601]# ls

1.py hostname.py hostname.pyc reg-v1.0 reg-v1.0.zip

[root@bak 201601]# cd reg-v1.0

[root@bak reg-v1.0]# ls

PKG-INFO setup.py utils

[root@bak reg-v1.0]# python setup.py install

running install

running build

running build_py

creating build

creating build/lib

creating build/lib/utils

copying utils/__init__.py -> build/lib/utils

copying utils/reg.py -> build/lib/utils

running install_lib

creating /usr/lib/python2.6/site-packages/utils

copying build/lib/utils/__init__.py -> /usr/lib/python2.6/site-packages/utils

copying build/lib/utils/reg.py -> /usr/lib/python2.6/site-packages/utils

byte-compiling /usr/lib/python2.6/site-packages/utils/__init__.py to __init__.pyc

byte-compiling /usr/lib/python2.6/site-packages/utils/reg.py to reg.pyc

running install_egg_info

Writing /usr/lib/python2.6/site-packages/reg-v1.0-py2.6.egg-info

[root@bak reg-v1.0]#

[root@bak reg-v1.0]#

安装后删除安装文件

[root@bak 201601]# ls

1.py hostname.py hostname.pyc reg-v1.0 reg-v1.0.zip

[root@bak 201601]# rm -rf reg*

测试安装

安装包utils下的reg.py文件有一个方法trm,该方法的作用为过滤掉一个字符串中的所有空白字符。测试如下

[root@bak 201601]# python

Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>> from utils.reg import trm

>>> s1 = trm('ab cd 123 ');

>>> s1

'abcd123'

>>>

1.3 简单的可执行文件

除了使用“python setup.py sdist”命令生成压缩包外,还可以简单地生成一些可执行安装程序。

使用bdist 命令创建单一的二进制Windows安装程序和Linux RPM文件;bdist可用格式有rpm与wininst。

python setup.py bdist --format=wininst

python 打包与部署

执行命令后,dist目录下多出一个可执行文件。

python 打包与部署

1.4 整个包及与之相关的所有文件打包

from distutils.core import setup

from setuptools import find_packages

setup(

name='reg',

version='v1.0',

packages=find_packages(exclude=['utils.*'])

)

1.5 整个项目

#coding=utf-8

'''

Created on 2016118

@author: fei

'''

from distutils.core import setup

from setuptools import find_packages

setup(

name='reg',

version='v1.0',

packages=find_packages(exclude=['*.*'])

)