You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在tox中运行bash脚本并复用其返回的库路径?

问题描述

创建tox环境时,触发环境不同,依赖库的安装路径会存在差异:

  • 在虚拟环境内触发tox:.tox/lib/site-packages
  • 在Docker内触发tox:.tox/lib/python3.8/site-packages

需要在tox后续步骤中复用该路径,因此编写了bash脚本用于查找已安装库的路径,希望在tox环境中运行脚本并将路径传递给后续命令,但尝试的配置无法正常运行。

尝试的tox.ini配置

[tox]
envlist =
    docs
min_version = 4
skipsdist = True
allowlist_externals = cd
passenv =
    HOMEPATH
    PROGRAMDATA
basepython = python3.8

[testenv:docs]
changedir = docs
deps =
    -r some_path/library_name/requirements.txt
commands =
    my_variable=$(bash ../docs/source/script.sh)
    sphinx-apidoc -f -o $my_variable/source $my_variable

报错信息

docs: commands[0] docs> my_variable=$(bash ../docs/source/script.sh)
docs: exit 2 (0.03 seconds) docs>
my_variable=$(bash../docs/source/script.sh) docs: FAIL code 2
(0.20=setup[0.17]+cmd[0.03] seconds) evaluation failed :( (0.50
seconds)

对应的script.sh脚本

#!/bin/bash

tox_env_path="../.tox/docs/"
conf_source="source"

tox_libs=$(find . $tox_env_path -type d -name "<name of library>")

sudo mkdir -p $tox_libs/docs/source
cp $conf_source/conf.py $conf_source/index.rst $tox_libs/docs/source

echo $tox_libs

解决方案

问题根源

Tox的commands默认按独立进程执行,第一个命令设置的环境变量无法传递给第二个命令;同时脚本中使用sudo会触发权限问题(tox通常以普通用户运行),路径查找逻辑也不够可靠。

修复步骤

  1. 合并命令到同一shell进程:用bash -c将两个命令封装,确保环境变量可复用
  2. 移除不必要的sudo:tox环境内操作无需管理员权限
  3. 优化路径获取逻辑:用Python内置模块直接获取site-packages路径,避免find遍历的低效和误差

修改后的tox.ini

[tox]
envlist =
    docs
min_version = 4
skipsdist = True
allowlist_externals = bash
passenv =
    HOMEPATH
    PROGRAMDATA
basepython = python3.8

[testenv:docs]
changedir = docs
deps =
    -r some_path/library_name/requirements.txt
commands =
    bash -c 'my_variable=$(bash ../docs/source/script.sh); sphinx-apidoc -f -o "$my_variable/source" "$my_variable"'

修改后的script.sh

#!/bin/bash

# 用Python内置模块精准获取当前环境的site-packages路径
site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
# 查找目标库的具体路径
tox_libs=$(find "$site_packages" -type d -name "<name of library>")

conf_source="source"
# 创建目录并复制文件,全程用双引号包裹路径避免空格问题
mkdir -p "$tox_libs/docs/source"
cp "$conf_source/conf.py" "$conf_source/index.rst" "$tox_libs/docs/source"

echo "$tox_libs"

额外说明

  • site.getsitepackages()会返回当前Python环境的标准site-packages路径,适配所有触发环境,无需手动判断路径格式
  • 所有路径用双引号包裹,防止路径包含空格时出现解析错误
  • 需在tox的allowlist_externals中添加bash,确保允许执行bash命令

内容的提问来源于stack exchange,提问作者QbS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:05:18