背景

开发了一个项目,需要打成pip包。

重要概念

pyproject.toml 就是“告诉 Python:这个项目叫什么、依赖什么、怎么打包、打完后生成什么”的配置文件。

步骤

1、目录结构

myproj/
  mypkg/
    __init__.py

2、准备project.toml

[project]
name = "mypkg"
version = "0.1.0"
description = "My package"
requires-python = ">=3.11"
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

3、准备构建工具。

python3 -m venv .pkgbuild
./.pkgbuild/bin/python -m ensurepip --upgrade
./.pkgbuild/bin/python -m pip install -U pip build hatchling -i https://pypi.tuna.tsinghua.edu.cn/simple

4、在项目根目录构建包
这里的“项目根目录”就是有 pyproject.toml 的目录

./.pkgbuild/bin/python -m build --no-isolation

最简模板

  1. 纯库包模板
    适合:别人 import mypkg,但没有命令行入口。

假设目录是:

myproj/
  pyproject.toml
  README.md
  mypkg/
    __init__.py

模板:

[project]
name = "mypkg"
version = "0.1.0"
description = "My Python package"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]
  1. 带 CLI 的包模板
    适合:安装后能直接运行命令,比如mytool。

假设目录是:

myproj/
  pyproject.toml
  README.md
  mypkg/
    __init__.py
    cli.py

如果 cli.py 里有一个可调用入口,例如:

def main():
    print("hello")

那模板可以写成:

[project]
name = "mypkg"
version = "0.1.0"
description = "My command line tool"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[project.scripts]
mypkg = "mypkg.cli:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

这里最关键的是这句:

[project.scripts]
mypkg = "mypkg.cli:main"

意思是安装后可以直接运行 mypkg 命令,它会调用 mypkg/cli.py 里的 main()。

  1. 带资源文件的包模板
    适合:除了 Python 代码,还要把脚本、配置、jar、模板等文件一起打进包里。这个最接近你现在的。

假设目录是:

myproj/
  pyproject.toml
  README.md
  bridge/
    package.json
  softwares/
    tool.jar
  mypkg/
    __init__.py
    cli.py
    resources/
      config.json
    skills/
      demo/
        SKILL.md
        scripts/
          run.sh
          helper.py

模板:

[project]
name = "mypkg"
version = "0.1.0"
description = "My package with bundled resources"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
  { name = "your name" }
]
dependencies = []

[project.scripts]
mypkg = "mypkg.cli:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["mypkg"]

[tool.hatch.build]
include = [
  "mypkg/**/*.py",
  "mypkg/**/*.md",
  "mypkg/**/*.sh",
  "mypkg/**/*.json",
]

[tool.hatch.build.targets.sdist]
include = [
  "mypkg/",
  "bridge/",
  "README.md",
]

artifacts = [
  "softwares/**",
]

[tool.hatch.build.targets.wheel.force-include]
"bridge" = "mypkg/bridge"
"softwares" = "mypkg/softwares"

这里最值得你记住的是:

include:选进正常资源
sdist.include:源码包里带什么
artifacts:专门处理像 softwares/ 这种可能被 .gitignore 忽略、但仍然要打包的内容
force-include:把包目录外的目录塞进最终 wheel

cd /tmp

wget https://github.com/loong64/docker-ce-packaging/raw/refs/heads/main/docker.service
wget https://github.com/loong64/docker-ce-packaging/releases/download/v27.4.1/docker-27.4.1.tgz
wget -O docker-buildx https://github.com/loong64/buildx/releases/download/v0.19.3/buildx-v0.19.3-linux-loongarch64
wget -O docker-compose https://github.com/loong64/compose/releases/download/v2.32.1/docker-compose-linux-loongarch64

mkdir -p /usr/local/libexec/docker/cli-plugins

tar -xf docker-27.4.1.tgz
sudo chown root:root docker/* docker-buildx docker-compose
sudo chmod 755 docker/* docker-buildx docker-compose
sudo mv docker.service /etc/systemd/system
sudo mv docker/* /usr/local/bin/
sudo mv docker-buildx /usr/local/libexec/docker/cli-plugins/
sudo mv docker-compose /usr/local/libexec/docker/cli-plugins/
sudo systemctl enable docker --now
sudo docker version
sudo docker buildx version
sudo docker compose version

删除docker

sudo systemctl stop docker docker.socket

sudo rm -rf /usr/local/libexec/docker/cli-plugins
sudo rm -f /usr/local/bin/docker*
sudo rm -f /usr/local/bin/container*
sudo rm -f /usr/local/bin/ctr
sudo rm -f /usr/local/bin/runc
sudo rm -f /etc/systemd/system/docker.service

背景

loong64架构的安同操作系统中安装微信后,无法使用中文输入法。

解决方法

1、编辑微信的快捷方式。

sudo vim /usr/share/applications/wechat.desktop

2、将正确的环境变量添加进去。
原来:

[Desktop Entry]
Name=wechat
Name[zh_CN]=微信
Exec=/usr/bin/wechat %U
StartupNotify=true
Terminal=false
Icon=/usr/share/icons/hicolor/256x256/apps/wechat.png
Type=Application
Categories=Utility;
Comment=Wechat Desktop
Comment[zh_CN]=微信桌面版

env GTK_IM_MODULE=fcitx QT_IM_MODULE=fcitx XMODIFIERS=@im=fcitx添加到Exec后面,如下所示:

[Desktop Entry]
Name=wechat
Name[zh_CN]=微信
Exec=env GTK_IM_MODULE=fcitx QT_IM_MODULE=fcitx XMODIFIERS=@im=fcitx /usr/bin/wechat %U
StartupNotify=true
Terminal=false
Icon=/usr/share/icons/hicolor/256x256/apps/wechat.png
Type=Application
Categories=Utility;
Comment=Wechat Desktop
Comment[zh_CN]=微信桌面版

3、保存,重启微信,问题解决。

背景

在VSCode中使用Roo Code时,遇到终端集成问题,Roo Code无法在终端中执行各种操作,也无法获取输出,非常不方便。

解决步骤

1、检查powershell版本。
按下 Win + R,输入 pwsh,如果报找不到文件pswh,则版本不对,需要升级。
2、在powershell命令行中输入以下命令,安装新版本。

winget search Microsoft.PowerShell
winget install --id Microsoft.PowerShell --source winget

3、用pswh方式打开新的powershell,执行以下命令。

New-Item -Path $PROFILE -ItemType File -Force

在创建的文件中写入以下内容:

if ($env:TERM_PROGRAM -eq "vscode") {
  . "$(code --locate-shell-integration-path pwsh)"
}

4、以管理员方式启动powershell,执行以下命令。

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

5、在VSCode中按下Ctrl + Shift + P,输入Preferences: Open User Settings (JSON),填入以下内容。

    "terminal.integrated.defaultProfile.windows": "PowerShell 7",
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ]
        },
        "PowerShell 7": {
            "path": "你的pwsh.exe路径",
            "args": []
        }
    },

6、在VScode终端中输入以下命令:

$PSVersionTable.PSVersion

看是否有正确输出。

参考资料

https://zhuanlan.zhihu.com/p/25724740375