你好,我是悦创。之前我在 CSDN 编写了一篇开发 Python 库的教程,有人加我提问到的一些问题,我来更新一下这篇文章:https://blog.csdn.net/qq_33254766/article/details/119874997
新版文章首发:https://bornforthis.cn/posts/18.html
你好,我是悦创。
Hello, I'm Yue Chuang.
我最近想要去开始开发 Python 第三方库,但是发现国内这样的教程太少了,所以就我来写吧!
I recently wanted to start developing Python third-party libraries, but found that there are too few such tutorials in the country, so I will write them instead!
还有就是曾经想创建一个 Python 库,无论是为您的工作团队还是在线的一些开源项目?在此博客中,您将学习如何操作!
Ever wanted to create a Python library, albeit for your team at work or for some open source project online? In this blog you will learn how to!
当您使用相同的开发工具 Pycharm ,你会最容易跟上我的教程,当然您也可以使用不同的工具。
The tutorial is easiest to follow when you are using the same tools, however it is also possible for you to use different ones.
本文章使用的是工具有:
The tools used in this tutorial are:
- MacOS「其实,不管你使用何种电脑都可以」MacOS command prompt
- Pycharm「社区版本足已」
第 0 步:MacOS 命令行命令「Step 0: MacOS command line command」
打开命令提示符并创建一个文件夹,您将在其中创建 Python 库。
Open your command prompt and create a folder in which you will create your Python library.
请记住:
Remember:
pwd
您可以看到您当前的工作目录。 「Withpwd
you can see your present working directory.」ls
您可以列出当前目录中的文件夹和文件。 「Withls
you can list the folders and files in your directory.」cd <path>
您可以更改当前所在的目录。 「Withcd <path>
you can change the current present directory you are in.」mkdir <folder>
您可以在当前工作目录中创建一个新文件夹。 「Withmkdir <folder>
you can create a new folder in your working directory.」
第 1 步:创建一个要放置库的目录「Step 1: Create a directory in which you want to put your library」
我创建一个文件夹名为:Turingaiyc,这个名称其实也是我后面发布库的名称,注意不要太普遍因为会重复,重复就会导致发布库失败。
I created a folder called Turingaiyc, which is actually the name of the library I will publish later. Be careful not to make it too common because it will be repetitive, which will cause the library to fail.
「PS:你如果 Win 系统的话,可以使用下面的命令,如果是 MacOS 的话我其实更喜欢命令行创建。」
I created a folder called Turingaiyc, which is actually the name of the library I will publish later. Be careful not to make it too common because it will be repetitive, which will cause the library to fail.
就我而言,我将使用的文件夹是 Turingaiyc
。将当前工作目录更改为您的文件夹。
In my case, the folder I will be working with is
Turingaiyc
. Change the present working directory to be your folder.
第 2 步:为您的文件夹创建一个虚拟环境「Step 2: Create a virtual environment for your folder」
在启动您的项目时,创建一个虚拟环境来封装您的项目总是一个好主意。虚拟环境由某个 Python 版本和一些库组成。
When starting your project, it is always a good idea to create a virtual environment to encapsulate your project. A virtual environment consists of a certain Python version and some libraries.
参考:这么全的 Python 虚拟环境?不看可惜了!
Reference: Such a full Python virtual environment? What a pity!
虚拟环境可防止以后遇到依赖性问题。 例如,在较旧的项目中,您可能使用的是较旧版本的 numpy 库。一些曾经运行良好的旧代码可能会在你更新 numpy 版本后不能正常运行了。 创建虚拟环境可以防止这种情况,当你与其他人协作时,虚拟环境也能确保你的程序在其他人的电脑上正常运行。 反之亦然。
Virtual environments prevent the issue of running into dependency issues later on. For example, in older projects you might have worked with older versions of the
numpy
library. Some old code, that once worked beautifully, might stop working once you update its version. Perhaps parts ofnumpy
are no longer compatible with other parts of your program. Creating virtual environments prevents this. They are also useful in cases when you are collaborating with someone else, and you want to make sure that your application is working on their computer, and vice versa.
接下来,你要确保你当前的工作目录是你刚刚创建的目录,( cd <path/to/folder>
) 中创建 Python 库的文件夹。)
(Make sure you changed the present working directory to the folder you are going to create your Python library in (
cd <path/to/folder>
).)
继续并通过键入以下内容创建虚拟环境:
代码语言:text复制Go ahead and create a virtual environment by typing:
python3 -m venv venv
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yy0tslPL-1656388442132)(./18.assets/image-20220617224826141.png)
创建后,你现在必须使用以下命令激活环境:
代码语言:text复制Once it is created, you must now activate the environment by using:
source venv/bin/activate
激活虚拟环境会修改 PATH 和 shell 的变量,以指向您创建的特定虚拟环境 Python 的设置。PATH 是 MacOS/Linux 和其他类 Unix 操作系统中的环境变量,它告诉 shell 在响应用户发出的命令时,去搜索哪些目录的 Python 执行环境(即准备运行的程序)。命令提示符将更改为通过添加 ( yourenvname) 来指示您当前所在的虚拟环境。
Activating a virtual environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created. PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user. The command prompt will change to indicate which virtual environment you are currently in by prepending (
yourenvname
).
你要确保你的环境已经安装了 pip、wheel、setuptools、twine。我们稍后将需要它们来构建我们的 Python 库。
代码语言:python代码运行次数:0复制In your environment, make sure you have pip installed
wheel
,setuptools
andtwine
. We will need them for later to build our Python library.
pip install wheel
pip install setuptools
pip install twine
或者也可以一条命了实现:
代码语言:text复制Or it can be done with one life:
sudo pip install wheel setuptools twine
我用的是 MacOS 所以,Windows 的话,去掉 sudo。
I'm using MacOS so for Windows, get rid of Sudo.
第 3 步:创建文件夹结构「Step 3: Create a folder structure」
这一步,也就是创建我们开发库所需要的文件。 在 Pycharm 中,打开您的文件夹 mypythonlibrary(或你自己创建的文件夹名称)。它应该是这样的:
In Pycharm, open your folder mypythonlibrary (or any name you have given your folder). It should look something like this:
你现在可以开始向项目添加文件夹和文件。您可以通过命令提示符或在 Pycharm 本身中执行此操作。
You now can start adding folders and files to your project. You can do this either through the command prompt or in Pycharm itself.
- 创建一个名为
setup.py
的空文件,这是创建 Python 库时最重要的文件之一!「Create an empty file calledsetup.py
. This is one of the most important files when creating a Python library!」 - 创建一个名为
README.md
的空文件,你可以在此处编写 Markdown 以向其他用户描述我们的库内容。「Create an empty file calledREADME.md
. This is the place where you can write markdown to describe the contents of your library for other users.」 - 创建一个名为
TuringRobots
,或者任何您希望在 pip 安装时调用 Python 库的文件夹。(如果你想稍后发布它,该名称在 pip 上应该是唯一的。)「Create a folder calledTuringRobots
, or whatever you want your Python library to be called when you pip install it. (The name should be unique on pip if you want to publish it later.)」 - https://pypi.org/search/?q=TuringRobots 后面的 TuringRobots 修改成你自己的库名字,之后打开浏览器访问,看是否有这个库。
- 在
TuringRobots
文件夹里面,创建名为__init__.py
的空文件。基本上,任何包含文件的__init__.py
文件夹,在我们构建它时,都将包含在库中。大多数情况下,您可以将__init__.py
文件留空,也就是不用写代码。在导入时,其中的__init__.py
里面的代码将被执行,因此它应该只包含能够运行项目所需的最少量代码。现在,我们将它保持原样。「Create an empty file insidemypythonlib
that is called__init__.py
. Basically, any folder that has an__init__.py
file in it, will be included in the library when we build it. Most of the time, you can leave the__init__.py
files empty. Upon import, the code within__init__.py
gets executed, so it should contain only the minimal amount of code that is needed to be able to run your project. For now, we will leave them as is.」 - 此外,在
TuringRobots
文件夹中,创建一个名为TuringRobots.py
「Also, in the same folder, create a file calledTuringRobots.py
.」 - 最后,在您的根文件夹中创建一个文件夹测试。在里面,创建一个空
__init__.py
文件和一个空的test_myfunctions.py
「And, finally, create a folder tests in your root folder. Inside, create an empty__init__.py
file and an emptytest_myfunctions.py
.」
你所创建的文件夹和代码文件,现在应如下所示:
Your set-up should now look something like this:
第 4 步:为您的库创建内容「Step 4: Create content for your library」
要将函数放入库中,您可以将它们放入 TuringRobots.py
文件中。例如,复制文件中的 TuringRobots 函数:
代码语言:python代码运行次数:0复制To put functions inside your library, you can place them in the
TuringRobots.py
file. For example, copy the TuringRobots function in your file:
# -*- coding: utf-8 -*-
# @Time : 2022/6/18 11:34
# @Author : AI悦创
# @FileName: TuringRobots.py
# @Software: PyCharm
# @Blog :https://bornforthis.cn/
import json
import urllib.request
import os
from dotenv import load_dotenv
def TuringRobots(text, over_print=True):
load_dotenv()
api_url = "http://openapi.tuling123.com/openapi/api/v2"
req = {
"reqType": 0,
"perception":
{
"inputText":
{
"text": text
},
"selfInfo":
{
"location":
{
"city": "厦门",
"province": "厦门",
"street": "海沧区"
}
}
},
"userInfo":
{
"apiKey": os.getenv("API_KEY"),
"userId": "OnlyUseAlphabet"
}
}
# print(req)
# 将字典格式的req编码为utf8
req = json.dumps(req).encode('utf8')
# print(req)
http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
response = urllib.request.urlopen(http_post)
response_str = response.read().decode('utf8')
# print(response_str)
response_dic = json.loads(response_str)
# print(response_dic)
intent_code = response_dic['intent']['code']
results_text = response_dic['results'][0]['values']['text']
if over_print:
print('Turing的回答:')
print('code:' str(intent_code))
print('text:' results_text)
else:
return (str(intent_code), results_text)
if __name__ == '__main__':
text = input("请输入你的对话:")
code, content = TuringRobots(text, over_print=False)
print(code, content)
上面实现了一个简单的机器人对话,对于 Python 测试,可以使用 pytest 和 pytest-runner 库。在虚拟环境中安装库:
代码语言:python代码运行次数:0复制A simple robot dialogue is implemented above, For testing with Python you can use the libraries
pytest
andpytest-runner
. Install the library in your virtual environment:
pip install pytest
pip install pytest-runner
pip install python-dotenv
让我们为项目创建一个小测试,测试如下:
代码语言:txt复制Let's create a small test for the project that looks like this:
TuringRobots_Tests.py
from TuringRobots import TuringRobots
def test_TuringRobots():
assert TuringRobots.TuringRobots("我是天才,那你呢?", over_print=True)
最后,让我们创建一个 setup.py
文件,它将帮助我们构建库。setup.py
的内容如下所示:
代码语言:python代码运行次数:0复制Finally, let’s create a
setup.py
file, that will help us to build the library. A limited version ofsetup.py
will look something like this:
from setuptools import find_packages, setup
setup(
name='mypythonlib',
packages=find_packages(),
version='0.1.0',
description='My first Python library',
author='Me',
license='MIT',
)
我的设置:【你们看简洁的配置即可】
代码语言:python代码运行次数:0复制# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time : 2022/6/18 11:51
# @Author : AI悦创
# @FileName: setup.py
# @Software: PyCharm
# @Blog :https://bornforthis.cn/
# Note: To use the 'upload' functionality of this file, you must:
# $ pipenv install twine --dev
import io
import os
import sys
from shutil import rmtree
from setuptools import find_packages, setup, Command
# Package meta-data.
NAME = 'TuringRobots'
DESCRIPTION = 'Simple dialogue test Turing robot.'
URL = 'https://github.com/AndersonHJB/TuringRobots'
EMAIL = 'bornforthis@bornforthis.cn'
AUTHOR = 'Bornforthis'
REQUIRES_PYTHON = '>=3.6.0'
VERSION = '1.2.5'
# What packages are required for this module to be executed?
REQUIRED = [
# 'dotenv==0.0.5',
]
# What packages are optional?
EXTRAS = {
# 'fancy feature': ['django'],
}
# The rest you shouldn't have to touch too much :)
# ------------------------------------------------
# Except, perhaps the License and Trove Classifiers!
# If you do change the License, remember to change the Trove Classifier for that!
here = os.path.abspath(os.path.dirname(__file__))
# Import the README and use it as the long-description.
# Note: this will only work if 'README.md' is present in your MANIFEST.in file!
try:
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = 'n' f.read()
except FileNotFoundError:
long_description = DESCRIPTION
# Load the package's __version__.py module as a dictionary.
about = {}
if not VERSION:
project_slug = NAME.lower().replace("-", "_").replace(" ", "_")
with open(os.path.join(here, project_slug, '__version__.py')) as f:
exec(f.read(), about)
else:
about['__version__'] = VERSION
class UploadCommand(Command):
"""Support setup.py upload."""
description = 'Build and publish the package.'
user_options = []
@staticmethod
def status(s):
"""Prints things in bold."""
print('