怎么构建一个Python包

发布时间:2021-10-28 15:22:24 作者:iii
来源:亿速云 阅读:210

本篇内容主要讲解“怎么构建一个Python包”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么构建一个Python包”吧!

开始

首先,您肯定需要设置一个或多个内容,以便了解如何构建python包。因此,需要的东西的如下:

构建Python包

本文中构建的标称为b_dist。b_dist是一个分布包,它有Gaussian, Binomial,  e.t.c等类。但首先,使用下面的映射结构创建包文件夹:

b_dist/         __init__.py        Binomialdistribution.py        Guassiandistribution.py        Generaldistribution.py        licence.txt        setup.cfgREADME.mdsetup.py

首先,您必须为上面在映射中列出的那些创建一个空文件。注意:b_dist是一个文件夹,setup.py带有自述文件。md不在b_dist文件夹中。

让我们先谈谈b_dist里面的文件:

b_dist/__init__.py

这个文件告诉python这个文件夹包含一个包。另外,包总是由init文件组成,即使它是空的。当您在python程序中导入一个包时,剩余的__init__文件就会运行。在这种情况下,其他的__init__文件导入高斯、二项和分布模型,以便在使用包时直接导入这些类。

b_dist/Binomialdistribution.py

二项文件是用于计算和可视化二项分布的类。

b_dist/Guassiandistribution.py

高斯文件是用于计算和可视化高斯分布的类。

b_dist/Generaldistribution.py

通用分布文件是用于计算和可视化概率分布的类。

b_dist/licence.txt

许可证档案实际上载有你的版权资料,说明你打算准许其他使用者自由使用你的套件。

b_dist/setup.cfg

cfg文件是一个保存自述文件数据的文件。

README.md

这是包的文档。它描述了包是如何工作的

setup.py

py文件是pip安装包所必需的。此外,它还包含关于包的元数据。请注意以下属性,如名称和包。这个属性必须与文件夹名' b_dist  '具有相同的值,以便在上传我们的包时避免bug。

本地运行包

让我们先通过输入下面的代码在本地运行包:

# change directory to where the setup file and the package is located ~$ cd python_package ~/python_package:$  # Install the package locally # NOTE:pip install . installs any setup.py file in that directory. ~/python_package:$ pip install .

输入后应该是输出:

Processing /python_package Building wheels for collected packages: b-dist   Building wheel for b-dist (setup.py) ... done   Created wheel for b-dist: filename=b_dist-0.4-py3-none-any.whl size=5108 sha256=d4c6f74daa1add07f37b01a74294e86ab07d655a6e0944bbb46ed6503ae493ef   Stored in directory: /tmp/pip-ephem-wheel-cache-3pvdd9ue/wheels/1e/f9/a3/568195cccd4e2d1dcb1edaf9c2708f651b90b6af6fbdfd3f36 Successfully built b-dist

最后,我们的包已经安装好了。让我们通过输入下面的代码来测试它是否有效:

# open the python shell In[1]: /python_package$ python Out[1] Python 3.7.3 (default, Mar 27 2019, 22:11:17)  [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. In[2] >>>from b_dist import Guassian In[3] >>> Guassian(10,5)Out[3] mean 10, standard deviation 5

太棒了! !,现在我们的包可以工作了,让我们继续进行下一个部分。

上传Python包

怎么构建一个Python包

使用PyPI可以快速上传新构建的包。首先来看什么是PyPi。PyPi代表Python包索引(PyPi),它是Python编程语言的软件存储库。

所以现在要将我们的包上载到PyPI站点的测试版本,另外要确保pip安装过程是否正常,然后现在要上载到PyPI站点。

首先,用测试创建一个帐户。对两个站点使用相同的用户名和密码。

在成功创建两个帐户之后,让我们返回IDE并将包上传到TestPyPi。但首先,要与这些站点通信,您需要pip安装一个名为twine的库,使用:

pip install twine

所以,安装twine后,输入以下代码先上传到TestPyPi:

# Creating the distribution package to be uploaded ~/python_package:$ python setup.py sdist

输入代码后,您将看到两个新文件夹,然后移动到下一行代码:

# Upload the package created using twinw ~/python_package:$ twine upload --repository-url https://test.pypi.org/legacy/ dist/* Output: Uploading distributions to https://test.pypi.org/legacy/ # enter your username and password used in registraion to the site Output: Enter your username : bideen Output: Enter your password : ########

输入密码后,你会看到一条成功的消息:“上传package_name  100%成功”。要检查上传是否成功,请访问您的TestPyPi帐户并查看您的新包。

现在使用以下代码从TestPyPi站点安装pip:

# first uninstall the previuos package on to aviod conflicts ~/python_package:$ pip uninstall b_dist # install fro the TestPyPi ~/python_package:$ pip install --index-url https://test.pypi.org/simple/ b_dist

成功集成TestPyPi之后,现在让我们继续上载到主PyPi,在那里可以使用包名直接进行pip安装,也可以公开使用。

# first uninstall the previuos package on to aviod conflicts ~/python_package:$ pip uninstall b_dist # install fro the TestPyPi ~/python_package:$ pip install b_dist

到此,相信大家对“怎么构建一个Python包”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. python如何构建一个生成器
  2. 怎么在Python中构建程序包

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:怎么实现SpringBoot缓存系统

下一篇:Mysql数据分组排名实现的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》