通过厨师配方激活virtualenv并安装python库

时间:2014-03-17 22:43:19

标签: python virtualenv chef

我有一个需要在virtualenv中安装的python包。这是一个虚拟服务器,它由下面显示的厨师食谱配置。我尝试了多种变化来激活virtualenv,但没有骰子。任何指针都赞赏。

python_virtualenv "/home/vagrant/myenv" do
  interpreter "python2.7"
  owner "vagrant"
  group "vagrant"
  action :create
end



directory "/usr/local/mysource" do
  owner "vagrant"
  group "vagrant"
  mode 0755
  action :create
end



git "/usr/local/mysource/django" do
  repository "https://github.com/django/django"
  revision "master"
  action :sync
end


script "install_script" do
  interpreter "bash"
  user "vagrant"
  cwd "/usr/local/mysource/django"
  code <<-EOH
  activate_env=os.path.expanduser("/home/vagrant/myenv/bin/activate_this.py")
  execfile(activate_env, dict(__file__=activate_env))
  python setup.py install
  EOH
end

3 个答案:

答案 0 :(得分:4)

我使用python_pip资源在virtualenvs中安装python包。这应该等同于pip install -e .

python_pip "/usr/local/mysource/django" do
  virtualenv "/home/vagrant/myenv"
  options '-e'
end

要在虚拟环境中运行其他命令(syncdb,collectstatic等),我经常使用这种模式:

virtualenv = "/home/vagrant/myenv"
bash "install_website" do
  code "#{virtualenv}/bin/python setup.py install"
  user "vagrant"
  cwd "/usr/local/mysource/django"
end

答案 1 :(得分:2)

python_pip是一本不推荐使用的食谱。请改用poise-python

安装最新版本的Python 2,然后使用它创建virtualenv并安装一些软件包:

directory "/opt/myapp" do
  owner "vagrant"
  group "vagrant"
  mode 0755
  action :create
end

python_runtime '2'

python_virtualenv '/opt/myapp/.env'

python_package 'Django' do
  version '1.8'
end

pip_requirements '/opt/myapp/requirements.txt'

答案 2 :(得分:0)

我不想使用过时且废弃的python / pip厨师食谱,因此设法手动完成。

  1. 创建shell脚本templates/default/flask_install_with_venv.sh.erb,并使用 template 将其放置在某个地方。
#!/bin/bash
source <%= @venv_path %>/bin/activate
pip3 install flask
pip3 install uwsgi
deactivate
template "/tmp/flask_install_with_venv.sh" do
  source "flask_install_with_venv.sh.erb"
  variables ({
    'venv_path' => venv_path
  })
end
  1. 使用以下命令执行脚本:
execute 'flask_install_with_venv' do
  command '/bin/bash /tmp/flask_install_with_venv.sh'
end