pycharm打包whl

文章目录

    • 报错
      • [报错 ERROR: *.whl is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).](#报错 ERROR: *.whl is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).)

类似于maven的打包功能,这样自己或别人就可以复用了。

本地打包whl

用一个例子来说明,例如打包一个加法工具。
1、新建一个项目,名称为:my_math_lib
2、创建文件夹my_math
(1)该文件夹下创建一个python文件,名为:__init__.py,内容如下:

bash 复制代码
# my_math/__init__.py
from .calculator import add, add_list

# 定义包的版本
__version__ = "0.1.0"

(2)该文件夹下创建一个python文件,名为:calculator.py,内容如下:

bash 复制代码
# my_math/calculator.py

def add(a, b):
    """
    两个数相加
    """
    return a + b

def add_list(numbers):
    """
    列表求和
    """
    return sum(numbers)

3、根目录下创建一个文件pyproject.toml,内容为:

bash 复制代码
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "my-math-lib"          # 【重要】发布到仓库后的包名 (通常用短横线)
version = "0.1.0"             # 版本号
description = "一个简单的加法库"
authors = [{name = "YourName", email = "your@email.com"}]
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
]

# 告诉 setuptools 去哪里找代码
[tool.setuptools.packages.find]
where = ["."]  # 在当前目录查找
include = ["my_math*"] # 包含以 my_math 开头的文件夹

4、终端执行python -m build --wheel,会生成dist目录以及whl文件。
5、到另外一个项目中,安装该whl。

bash 复制代码
pip install -e D:/PycharmProjects/my_math_lib

注:这里是路径,不是具体的whl文件,否则会报错。

6、在另外一个项目中新建test.py,内容如下:

bash 复制代码
import my_math

result = my_math.add(10, 20)
print(f"10 + 20 = {result}")

# 如果配置了 __init__.py 导出,也可以直接:
# from my_math import add

7、运行test.py,实测成功了。

报错

报错 ERROR: *.whl is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).

重点就是这句: It should either be a path to a local project or a VCS URL

意思是不是一个具体whl文件,还是whl所在的项目路径。

相关推荐
李昊哲小课1 分钟前
咖啡工坊 · FastAPI 22 章教程合集
服务器·人工智能·python·fastapi
学废了wuwu2 分钟前
从入门到实战:深入浅出模型压缩三大核心——剪枝、彩票假设与知识蒸馏
人工智能·算法·剪枝
Microvision维视智造11 分钟前
从“看见“到“看懂“——工业视觉的大模型时刻到了吗?
人工智能·计算机视觉·视觉检测·机器视觉
janeboe18 分钟前
抖音黑科技兵马俑总站源头简博科技 | 抖音锚点不合规挂载新规今日生效,短视频引流直播间迈入三端全链路监管时代
大数据·人工智能·科技
卷无止境21 分钟前
Python 异常处理:从入门到工程实践
后端·python
lialaka21 分钟前
「未来星途(Future_Odyssey)」——全语音_3D_AI_具身交互智能数字教官与全双工星际科普沉浸舱[1]
人工智能·3d·交互
hongmai66688822 分钟前
ESP32-C61-WROOM-1-N8R2:Wi-Fi 6与RISC-V融合的中坚力量
人工智能·单片机·嵌入式硬件·物联网·智能家居·risc-v
garuda herb23 分钟前
生成专题页Blog--建立并返回 MySQL 数据库连接
python·mysql
正在走向自律25 分钟前
用豆包Seed Evolving打造全功能【AI智能记账】小程序,开源可落地
人工智能·小程序·开源·智能记账
ONE_SIX_MIX27 分钟前
qwenpaw-plugin-file-tools QwenPaw增强文件工具 更新 v1.1.1
python·qwenpaw