Search code examples
pythondjangoansiblerhel

Django installation fails using Ansible in RHEL


I have the following playbook:

---
- hosts: app
  become: yes
  tasks:
    - name: Install MySQL-Python
      yum: name=MySQL-python state=present

    - name: Install Python Setup Tools
      yum: name=python-setuptools state=present

    - name: Install django
      easy_install: name=django state=present

This fails with the error:

This version of Django requires Python 3.4, but you're trying to\ninstall it on Python 2.7.\n\nThis may be because you are using a version of pip that doesn't\nunderstand the python_requires classifier. Make sure you\nhave pip >= 9.0 and setuptools >= 24.2, then try again:\n\n $ python -m pip install --upgrade pip setuptools\n $ python -m pip install django\n\nThis will install the latest version of Django which works on your\nversion of Python. If you can't upgrade your pip (or Python), request\nan older version of Django:\n\n $ python -m pip install \"django<2\"\nerror: Setup script exited with 1\n"}

I followed this article to install Python 3 and also set python=python3, yet I am facing the same error message when I run the playbook.

Can anyone please suggest what to do? Also, I do I install a previous version of Django using Ansible?


Solution

  • I would use the pip module to install Django instead of easy_install. You can use executable to use the pip for Python 3.4. You can use version to specify which version you want to use - if you decide to use Python 2 you will need to install Django<2.

    - name: Install django
      pip: 
        name: django
        executable: pip-3.4
        version: 2.0.4
    

    Note that MySQL-Python has not been supported in several years. It would be better to use the fork mysqlclient, which supports Python 3.

    You should also consider installing your modules in a virtual environment.