📜  如何构建Python包?

📅  最后修改于: 2022-05-13 01:54:54.744000             🧑  作者: Mango

如何构建Python包?

在本文中,我们将学习如何在Python开发包。包只不过是一组旨在执行特定任务集的程序。包有两种类型,即

  • 内置包,如集合、日期时间、sqlite 等。
  • 外部包,如烧瓶、django、tensorflow 等。

创建包

首先,我们需要想一种方法来构建我们的代码,以便其他人可以访问我们的代码功能。在Python,要制作一个包,我们需要在目录中添加一个__init__.py 。在这里,我们将制作一个名为test_package的包。

  • 让我们编写__init__.py
Python3
from collections import Counter
  
def count_in_list(l, word):
  c = Counter(l)
  return c[word]


Python3
from test_package import count_in_list
  
l = ["gfg", "dsa", "gfg"]
count = count_in_list(l, "gfg")
print(count)


Python3
import setuptools
  
with open("README.md", "r") as fh:
    description = fh.read()
  
setuptools.setup(
    name="test-package",
    version="0.0.1",
    author="GeeksforGeeks",
    author_email="contact@gfg.com",
    packages=["test_package"],
    description="A sample test package",
    long_description=description,
    long_description_content_type="text/markdown",
    url="https://github.com/gituser/test-tackage",
    license='MIT',
    python_requires='>=3.8',
    install_requires=[]
)


Python3
from test_package import count_in_list
  
print(count_in_list(["gfg", "dsa", "gfg"], "gfg")) # output: 2
print(count_in_list(["gfg", "dsa", "gfg"], "maths")) # output: 0


  • 现在,创建一个名为test_package的目录,并在其中保存__init__.py文件。就这样,我们的包裹准备好了。我们所有的包都会计算一个单词在列表中出现的次数。现在,要使用我们的包,请在test_package目录外创建一个run.py文件。在run.py 中,只需导入新创建的包并使用count_in_list函数。我们可以编写如下所示的代码。

蟒蛇3

from test_package import count_in_list
  
l = ["gfg", "dsa", "gfg"]
count = count_in_list(l, "gfg")
print(count)
  • 就这样。我们刚刚创建了我们的第一个包。我们的目录结构应该是这样的
package-folder
├── run.py
└── test_package
    └── __init__.py

1 directory, 2 files

要测试它,只需键入python3 run.py 和O乌尔输出应该如下:



通过这种方式,我们可以创建许多复杂的程序并在其他代码中使用它们。

上传我们的包

现在,让我们看看如何准备要在PyPI上部署的包。我们需要在我们的包文件夹中添加一些额外的文件,比如README.MDLICENSEsetup.py 。我们的代码结构如下。

package-folder
├── LICENSE
├── README.md
├── setup.py
└── test_package
    └── __init__.py

1 directory, 4 files

确保删除之前创建的run.py ,它仅用于手动测试。对于 LICENSE,我们建议使用 MIT LICENSE,因为它提供了最大的灵活性。我们可以在线阅读各种类型的许可证。

接下来,创建 README.md 文件。它基本上包含对包的完整描述。如果我们不熟悉 Markdown 风格的写作,我们推荐阅读这篇文章。一旦,我们准备好您的 README.md 文件,我们需要编写setup.py 。这是最重要的部分。

蟒蛇3

import setuptools
  
with open("README.md", "r") as fh:
    description = fh.read()
  
setuptools.setup(
    name="test-package",
    version="0.0.1",
    author="GeeksforGeeks",
    author_email="contact@gfg.com",
    packages=["test_package"],
    description="A sample test package",
    long_description=description,
    long_description_content_type="text/markdown",
    url="https://github.com/gituser/test-tackage",
    license='MIT',
    python_requires='>=3.8',
    install_requires=[]
)

在上面的代码中,您需要更改

  • 有你名字的作者
  • author_email与您的电子邮件
  • url与您的包的 GitHub URL

我们的包裹现在准备好了。

将我们的包注册到 PyPI

现在我们开发了我们的Python包,我们需要在 PyPI 上注册它。



1.上传到GitHub

创建一个新的 GitHub 存储库并将我们所有的代码推送到那里。如果您不知道如何将代码推送到 GitHub 存储库,可以前往阅读本文。另外,不要忘记使用新创建的 GitHub 存储库 URL 更新我们在setup.py中的 URL。我们的 repo 应该可以公开访问。

2. 在 PyPI 中创建一个帐户

我们要在 PyPI 中发布包,我们需要一个帐户。为此,只需访问 PyPI 并创建您的帐户。

3. 生成分布

需要分发档案才能将其作为包托管。要生成这些包,我们需要安装两个额外的包。

pip3 install setuptools wheel

现在我们安装了它们,运行以下命令来生成档案:

python3 setup.py sdist bdist_wheel

它将生成文件夹builddisttest_package.egg-info 。现在你的目录结构应该是这样的。

package-folder
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── test_package
│           └── __init__.py
├── dist
│   ├── test_package-0.0.1-py3-none-any.whl
│   └── test-package-0.0.1.tar.gz
├── LICENSE
├── README.md
├── setup.py
├── test_package
│   └── __init__.py
└── test_package.egg-info
    ├── dependency_links.txt
    ├── PKG-INFO
    ├── SOURCES.txt
    └── top_level.txt

7 directories, 11 files

4. 部署

要上传到 PyPI,我们需要安装 twine 包。



pip3 install twine

现在,使用setup.py文件中定义的正确版本上传包。为此,请运行以下命令

twine upload --repository pypi dist/*

这就是在Python开发和部署包的全部内容。

使用我们的包

包开发完成后,应该就可以使用了吧?毕竟,它的开发目的是在不同的代码库中重用相同的逻辑。创建一个全新的目录并在其中创建一个虚拟环境。为此,只需键入

python3 -m venv env
source env/bin/activate

现在,将新部署的包安装到虚拟环境中。您需要使用pip才能安装它。

pip install test-package

现在,让我们看看如何在我们的代码中使用这个包。这个想法很简单。这毕竟是一个包裹,对吧?所以,我们需要做的就是将它作为一个包导入。

from test_package import count_in_list

现在,我们有了我们的函数。让我们尝试使用它。

print(count_in_list(["gfg", "dsa", "gfg"], "gfg")) # output: 2
print(count_in_list(["gfg", "dsa", "gfg"], "maths")) # output: 0

就这样。如果我们把它们放在一起

蟒蛇3

from test_package import count_in_list
  
print(count_in_list(["gfg", "dsa", "gfg"], "gfg")) # output: 2
print(count_in_list(["gfg", "dsa", "gfg"], "maths")) # output: 0

输出:

这就是开发包并将其部署到 PyPI 的全部内容。