Heim  >  Artikel  >  Backend-Entwicklung  >  Python代码的打包与发布详解

Python代码的打包与发布详解

WBOY
WBOYOriginal
2016-06-06 11:31:221036Durchsuche

在python程序中,一个.py文件被当作一个模块,在各个模块中定义了不同的函数。当我们要使用某一个模块中的某一个函数时,首先须将这个模块导入,否则就会出现函数未定义的情况.

下面记录的是打包及安装包的方法。

本文示例是建立一个模拟登录的程序:

logIn.py文件代码如下:

pwd=int(raw_input('please input your passward: '))
if pwd==123:
  print 'success'
else:
  print 'error'

一、打包

1.先建立一个文件夹,这个文件夹用来存放我们将要用于发布的.py文件,(现在我们建立一个文件夹名为distribution,将logIn.py放在这个文件夹中)

2.在distribution文件夹中新建一个 setup.py文件代码如下:

from distutils.core import setup
setup(
 name='logIn',  #这个是最终打包的文件名
 version='1.0.0',
 py_modules=['logInr'], #要打包哪些,.py文件,
 )

3.在最终中,cd到这个distrbution文件夹中,然后运动行如下命令:

python setup.py sdist

这样在文件夹中就多出了几个文件,在dist文件夹中的,logIn-1.0.0.tar.gz就是我们的发布包了;

二、安装包到本地副本中:

sudo python setup.py install

路径为:/usr/local/lib/python2.7/dist-packages

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn