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

如何在Chef Recipe中配置Python虚拟环境?解决pip路径不存在报错

问题解决:Chef创建Python虚拟环境后找不到pip的问题

问题背景

我需要将代码仓库克隆到目标机器并安装Python依赖,但以root用户运行pip时收到权限警告,尝试创建虚拟环境消除警告时,出现错误:

had an error: Errno::ENOENT: No such file or directory - /var/log/virtual-environment/bin/pip

以下是原属性文件和配方代码:

属性文件(attributes/default.rb)

# Cookbook: chefcookbookname
# Attribute file: attributes/default.rb

default['chefcookbookname']['venv_path'] = '/var/log/virtual-environment'

原配方代码(recipes/default.rb)

# Cookbook:: chefcookbookname
# Recipe:: default


package 'git'

package 'python3-pip'

package 'python3.10-venv'

directory '/var/log/tmp' do
  action :create
  recursive true
end

directory '/var/log/myrepo' do
  action :create
  recursive true
end

directory '/var/log/virtual-environment' do
  action :create
  recursive true
end

venv_path = node['chefcookbookname']['venv_path']

directory venv_path do
  action :create
end

execute 'create_virtualenv' do
  command "python3 -m venv #{venv_path}"
  not_if { ::File.exist?(venv_path) }
end

git 'clone_repository' do
  repository 'https://repo.git'
  reference 'main'
  destination '/var/log/myrepo'
  action :sync
end

execute 'move_requirements' do
  command "mv /var/log/myrepo/requirements.txt #{venv_path}"
  action :run
  only_if { ::File.exist?("/var/log/myrepo/requirements.txt") }
end

execute 'install_python_dependencies' do
  command "#{venv_path}/bin/pip install -r #{venv_path}/requirements.txt"
  only_if { ::File.exist?("#{venv_path}/requirements.txt") }
  environment(
    'VIRTUAL_ENV' => venv_path,
    'PATH' => "#{venv_path}/bin:#{ENV['PATH']}"
  )
end

错误原因

  1. 虚拟环境创建条件错误:not_if { ::File.exist?(venv_path) }仅判断目录是否存在,若目录已被手动创建但未生成虚拟环境,会跳过创建命令,导致venv目录下没有bin/pip。
  2. 冗余目录创建:手动提前创建虚拟环境目录,可能干扰python3 -m venv的正常初始化流程。
  3. 不必要的文件移动:将requirements.txt移动到venv目录,既污染venv结构,也可能因权限问题导致后续读取失败。

修改后的配方代码

# Cookbook:: chefcookbookname
# Recipe:: default

package 'git'
package 'python3-pip'
package 'python3.10-venv'

# 创建临时目录和仓库目录
directory '/var/log/tmp' do
  action :create
  recursive true
end

directory '/var/log/myrepo' do
  action :create
  recursive true
end

venv_path = node['chefcookbookname']['venv_path']

# 检查虚拟环境是否已正确初始化(判断bin/pip是否存在)
execute 'create_virtualenv' do
  command "python3 -m venv #{venv_path}"
  not_if { ::File.exist?("#{venv_path}/bin/pip") }
end

git 'clone_repository' do
  repository 'https://repo.git'
  reference 'main'
  destination '/var/log/myrepo'
  action :sync
end

# 直接从仓库目录读取requirements.txt,无需移动文件
execute 'install_python_dependencies' do
  command "#{venv_path}/bin/pip install -r /var/log/myrepo/requirements.txt"
  only_if { ::File.exist?("/var/log/myrepo/requirements.txt") }
  environment(
    'VIRTUAL_ENV' => venv_path,
    'PATH' => "#{venv_path}/bin:#{ENV['PATH']}"
  )
end

修改说明

  • 修正虚拟环境创建判断:改为检查#{venv_path}/bin/pip是否存在,确保即使目录提前创建,只要虚拟环境未生成就会执行创建命令。
  • 移除冗余目录创建:删除手动创建虚拟环境目录的代码,由python3 -m venv自动处理目录生成,避免干扰初始化流程。
  • 优化依赖安装路径:直接从克隆的仓库读取requirements.txt,无需移动文件,保持venv目录结构整洁,减少权限风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 11:53:14