Question

I have a python package that needs to be installed in virtualenv. This is a virtual server and it is provisioned by chef recipes displayed below. I tried multiple variations to activate the virtualenv but no dice. Any pointers appreciated.

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
Était-ce utile?

La solution

I use the python_pip resource to install python packages in virtualenvs. This should be equivalent to pip install -e .:

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

To run other commands (syncdb, collectstatic, etc) in a virtual environment, I often use this pattern:

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

Autres conseils

python_pip is a deprecated cookbook. Use poise-python instead.

To install the latest available version of Python 2 and then use it to create a virtualenv and install some packages:

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'

I didn't want to use pretty outdated and abandoned python/pip chef cookbooks so managed to do it manually.

  1. Create shell script templates/default/flask_install_with_venv.sh.erb and put it somewhere with 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 the script with:
execute 'flask_install_with_venv' do
  command '/bin/bash /tmp/flask_install_with_venv.sh'
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top