Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Ansible pip update python package during development

I have been writing some Ansible plays to setup a python virtualenv and also during development to update the python package and restart the server. I am having problems though getting pip to update a package. I don't really care how this is done, but I would prefer during development just adding the path to the python path in the virtualenv and then just restarting the server, but I haven't figured out how to do that in Ansible yet.

So my question is how can I setup a local git repo that either installs into site-packages of the virtualenv or setup Ansible to sys.path.insert the location of the repo using the correct virtualenv.

Currently I was trying to do:

sudo pip install ~/workspace/python-repo

Before I install the package I renamed a class that I have from Authenticator to something completely wrong like Authen. The class shows Authen during a fresh install. Then I change the class name back to the correct name (Authenticator), bump the version and run

sudo pip install ~/workspace/python-repo --upgrade

but after inspecting the actual file in site-packages it still shows the Authen name rather than the updated file.

How can I make this so that I use a local repo during development and get instant updated files in my environment? As well as making this a repeatable process through Ansible.

Here is what I am trying to do in Ansible. My first play is setting up the environment which I only want to run 1 time.

- name: Install python-repo
local_action: pip name=${python_root}
                  virtualenv=${working_dir}/development

${python_root} is only the location to my python project and of course the working directory is the new virtualenv setup.

Then somehow I want a development play to update the python repo in the virtualenv. This is what I have so far but this doesn't work either.

- name: Update python-repo
local_action: pip  name=${python_root}
                   virtualenv=${working_dir}/development
                   state=latest

- name: Restart services.
  local_action: service name=${item} state=restarted
  with_items: ${services} 
like image 662
lumberjacked Avatar asked Sep 12 '13 14:09

lumberjacked


People also ask

How do I upgrade a Python package using pip?

Update a package: pip install --upgrade To update installed packages to the latest version, run pip install with the --upgrade or -U option.

How do you upgrade pip on Ansible?

After passing the installation of pip software , you can install Ansible. Use pip with sudo command in order to implement global changes to the system. Since pip does not coordinate with system package managers, it could make changes to your system that leaves it in an inconsistent or non-functioning state.

Does pip automatically install with Python?

PIP is automatically installed with Python 2.7.9+ and Python 3.4+ and it comes with the virtualenv and pyvenv virtual environments.


1 Answers

I am going to post this as the correct answer for everyone else to have a reference back to this.

Currently I have an Ansible play that sets up an environment by installing local python packages and then creating a virtualenv and installing everything into that for development. First in setting up the virtualenv and install your local git repo into the environment i use these tasks in Ansible.

- name: Source virtualenvwrapper.
  local_action: shell /usr/local/bin/virtualenvwrapper.sh 
                    executable=/bin/bash

- name: Set Enviroment to working directory.
  local_action: shell export WORKON_HOME=${working_dir}

- name: Set pip to use working virtual enviroment.
  local_action: shell export PIP_VIRTUALENV_BASE=$WORKON_HOME

- name: Create new virtualenv in development.
  local_action: pip requirements=${virtual_requirements} 
                  virtualenv=${working_dir}/development

- name: Install python-repo as editable 
  local_action: pip name=${python_root}
                  virtualenv=${working_dir}/development
                  extra_args='-e ${python_root}' 

Now I start developing and when I'm ready to test things and run it I use a different Ansible play to deploy local changes. I use these tasks.

- name: Update python-repo
  local_action: pip name=${python_root}
                  virtualenv=${working_dir}/development
                  extra_args='--upgrade'

- name: Restart services.
  local_action: service name=${item} state=restarted
  with_items: ${services}

This accepts my python updates immediately and restarts my server. I banged my head around this for a long time coming from a php development environment and only needing to press F5 to accept changes. I wanted a good python development environment and I think this is an acceptable process.

like image 155
lumberjacked Avatar answered Sep 17 '22 18:09

lumberjacked