Skills Dependency Management
Skill scripts usually require third-party libraries. How to declare, install, and lock these dependencies is the foundation for ensuring reliable Skill operation.
Dependency Declaration Files
Create requirements.txt (Python) or package.json (Node.js) in the Skill's scripts/ directory to declare all dependencies centrally.
Python dependencies: requirements.txt
# File path: scripts/requirements.txt
# Format: package==version (lock version for reproducible installation)
pandas==2.1.4 # 数据处理
openpyxl==3.1.2 # Excel 读写
pypdf==3.17.0 # PDF 处理
python-docx==1.1.0 # Word 文档处理
requests==2.31.0 # HTTP 请求
Node.js dependencies: package.json
Example
{
"name"
:
"my-skill-scripts"
,
"version"
:
"1.0.0"
,
"description"
:
"Node.js dependencies for the Skill script"
,
"dependencies"
:
{
"xlsx"
:
"^0.18.5"
,
"puppeteer"
:
"^21.0.0"
}
}
Explain the installation method in SKILL.md
## Dependency Installation
Before first use, run the following command to install Python dependencies:
```bash
pip install -r scripts/requirements.txt --break-system-packages
If the runtime environment already has the dependencies pre-installed, you can skip this step.
---
## 在脚本中自动安装依赖
对于需要零配置运行的 Skill,可以在脚本开头加入依赖自检和自动安装逻辑。
## 实例
# File path: scripts/auto_deps.py
import
subprocess
import
sys
import
importlib
# Define dependency list: (pip install name, import name, version requirement)
DEPENDENCIES
=
[
(
"pandas"
,
"pandas"
,
">=2.0"
)
,
(
"openpyxl"
,
"openpyxl"
,
">=3.0"
)
,
(
"python-docx"
,
"docx"
,
None
)
,
# No version requirement
]
def
install_if_missing
(
pip_name:
str
,
import_name:
str
)
-
>
bool
:
"""检查并按需安装依赖包"""
try
:
importlib.
import_module
(
import_name
)
return
True
# Installed, return directly
except
ImportError
:
print
(
f
"正在安装:{pip_name}..."
)
result
=
subprocess
.
run
(
[
sys
.
executable
,
"-m"
,
"pip"
,
"install"
,
pip_name
,
"--break-system-packages"
,
"--quiet"
]
,
capture_output
=
True
,
text
=
True
)
if
result.
returncode
==
0
:
print
(
f
"安装成功:{pip_name}"
)
return
True
print
(
f
"安装失败:{pip_name}\n{result.stderr}"
)
return
False
def
ensure_all_deps
(
)
-
>
bool
:
"""确保所有依赖已安装"""
failed
=
[
]
for
pip_name
,
import_name
,
_
in
DEPENDENCIES:
if
not
install_if_missing
(
pip_name
,
import_name
)
:
failed.
append
(
pip_name
)
if
failed:
print
(
f
"以下依赖安装失败:{', '.join(failed)}"
)
return
False
return
True
if
__name__
==
"__main__"
:
if
not
ensure_all_deps
(
)
:
sys
.
exit
(
1
)
# Execute normal logic after dependencies are ready
import
pandas
as
pd
print
(
"所有依赖已就绪,开始执行..."
)
Installing: python-docx... Installed successfully: python-docx All dependencies are ready, starting execution...
---
## Version conflict handling strategy
When the package version required by the Skill conflicts with the existing system version, there are the following ways to handle it:
| 策略 | 做法 | 适用场景 |
| --- | --- | --- |
| 宽松版本要求 | 使用 >=而非==,允许更高版本 | 大多数场景,兼容性好 |
| 虚拟环境 | 在 venv 中安装,与系统隔离 | 对版本有严格要求 |
| 使用系统已有版本 | 不锁定版本,使用系统安装的版本 | 依赖为常用库且版本差异影响小 |
### Create virtual environment (optional)
## 实例
# Create a virtual environment in the Skill directory
python3
-m
venv scripts
/
.venv
# Activate virtual environment
source
scripts
/
.venv
/
bin
/
activate
# Install dependencies in the virtual environment
pip
install
-r
scripts
/
requirements.txt
# Run script in virtual environment
python scripts
/
process.py
> 在 Claude 的执行环境中,通常不需要虚拟环境,因为每次会话都是相对隔离的。虚拟环境主要用于本地开发阶段,避免与开发机器上的其他项目产生冲突。
---
## 依赖文档规范
在 SKILL.md 或单独的 README 中,应清晰列出所有外部依赖,便于用户在安装前了解要求。
System Requirements
Runtime environment
- Python 3.8 or higher
- pip (Python package management tool)
Python dependencies
| Package name | Version | Purpose |
|---|---|---|
| pandas | >=2.0 | Data reading and processing |
| openpyxl | >=3.0 | Excel file reading and writing |
| python-docx | Any | Word document generation |
System tools (optional)
- pandoc: format conversion (apt-get install pandoc)