分类 新教程 下的文章

背景

开发了一个项目,需要打成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

背景

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

背景

每次AirPods连接MAC都会被音量吓一跳。

步骤

1、安装Hammerspoon。

brew install hammerspoon --cask

2、创建默认配置。

mkdir -p ~/.hammerspoon
touch ~/.hammerspoon/init.lua

3、在init.lua中写入配置

-- 存储上一次默认音频输出设备的名称
local lastOutputDeviceName = hs.audiodevice.defaultOutputDevice():name()

-- 处理设备变更的回调函数
local function handleDeviceChanges()
-- 获取当前默认音频输出设备的名称
local currentOutputDeviceName = hs.audiodevice.defaultOutputDevice():name()
-- 判断设备名称是否发生了变化
if lastOutputDeviceName ~= currentOutputDeviceName then
-- 判断当前设备是否为蓝牙耳机
if string.find(currentOutputDeviceName, "AirPods") then
-- 连接蓝牙耳机时将音量设置为 20
hs.audiodevice.defaultOutputDevice():setVolume(20)
hs.alert.show("音量已设为 20")
-- 判断上一个设备是否为蓝牙耳机
elseif string.find(lastOutputDeviceName, "AirPods") then
-- 断开蓝牙耳机时将音量设置为 0
hs.audiodevice.defaultOutputDevice():setVolume(0)
hs.alert.show("音量已设为 0")
end
end
-- 更新 lastOutputDeviceName 为当前设备名称
lastOutputDeviceName = currentOutputDeviceName
end

-- 为音频设备变动设置回调函数
hs.audiodevice.watcher.setCallback(handleDeviceChanges)
-- 启动音频设备监听器
hs.audiodevice.watcher.start()

4、重新加载配置。
在启动台搜索Hammerspoon,启动应用。然后在标题栏点击他的图标,然后选择Reload Config。

参考资料

https://www.v2ex.com/t/1011644

1 字体下载

wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf &&
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf  &&
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf  &&
wget https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf

分别下载以上字体,并双击安装

2 在vscode中设置字体

在设置中搜索font,然后找到Features-Terminal,将其中的Font-Family设置为MesloLGS NF,重启VSCode即可。

3 其他远程连接软件

同样将字体设置为MesloLGS NF,重启即可。

4 好用插件

默认提供以下功能:
z 关键词:快速跳转到含有上面关键词的路径中
命令自动补全:输入关键词,然后按方向右键,自动补全之前执行过的命令。