Search code examples
djangovagrantpipansible

Install pip3 package using ansible instead of pip2


I am trying to setup a Django project in vagrant using ansible. I have used the following code for installing the pip packages:

- name: Setup Virtualenv
  pip: virtualenv={{ virtualenv_path }} virtualenv_python=python3 requirements={{ virtualenv_path }}/requirements.txt

I need to use python3 for the django project and even though I have explicitly mentioned to use python3, it is installing the pip packages via pip2. I have ensured that python3 is installed on the virtual machine.

Please, help me install the packages via pip3.


Solution

  • Try to use executable option. Excerpt from pip module doc:

    executable (added in 1.3)

    The explicit executable or a pathname to the executable to be used to run pip for a specific version of Python installed in the system. For example pip-3.3, if there are both Python 2.7 and 3.3 installations in the system and you want to run pip for the Python 3.3 installation. It cannot be specified together with the 'virtualenv' parameter (added in 2.1). By default, it will take the appropriate version for the python interpreter use by ansible, e.g. pip3 on python 3, and pip2 or pip on python 2.

    Update:

    To combine virtualenv path and alternative executable, use virtualenv_command like this:

    - pip:
        virtualenv: /tmp/py3
        virtualenv_command: /usr/bin/python3 -m venv
        name: boto
    

    Absolute path is required for virtualenv_command.