+
95
-

python如何自己写一个库然后安装到python库中调用?

我们经常用pip instal 安装python的库,那么python如何自己写一个库然后安装到python库中调用?


网友回复

+
15
-

要自己写一个Python库并安装到Python库中调用,可以按照以下步骤进行:

1. 创建项目结构

首先,创建一个项目目录,并在其中创建必要的文件和文件夹。例如:

mylib/
├── mylib/
│   ├── __init__.py
│   └── mymodule.py
├── tests/
│   └── test_mymodule.py
├── setup.py
└── README.md
2. 编写模块代码

在mylib/mymodule.py中编写你的模块代码。例如:

# mylib/mymodule.py
def greet(name):
    return f"Hello, {name}!"
3. 初始化包

在mylib/__init__.py中初始化你的包。例如:

# mylib/__init__.py
from .mymodule import greet
4. 编写setup.py

在项目根目录下创建setup.py文件,用于描述你的包。例如:

# setup.py
from setuptools import setup, find_packages

setup(
    name='mylib',
    version='0.1',
    packag...

点击查看剩余70%

我知道答案,我要回答