ImportError: No module named pymongo
<p>I'm trying to install <a href="http://api.mongodb.com/python/current/" rel="nofollow">PyMongo</a> Python package with pip. It is required by <a href="https://docs.ansible.com/ansible/mongodb_user_module.html" rel="nofollow">Ansible mongodb_user module</a>.</p>

<p>I'm installing pip and pymongo with following Ansible script:</p>

<pre><code>- hosts: tag_Name_Development
  become: true
  remote_user: user
  tasks:
    - name: install python tools
      yum: name={{ item }} state=latest
      with_items:
        - gcc
        - python-devel
        - python-setuptools
        - python-pip

    - name: install pymongo
      pip: name=pymongo state=latest

    - name: add admin user to mongo
      mongodb_user:
        login_port: 27017
        database: admin
        name: admin
        password: "{{ mongodb.admin_pass }}"
        roles: userAdminAnyDatabase
        state: present
</code></pre>

<p>After successful installation of tools I get following Ansible error.</p>

<p><code>FAILED! =&gt; {"changed": false, "failed": true, "msg": "the python pymongo module is required"}</code></p>

<p>On server where <code>pymongo</code> is installed I get</p>

<pre><code>$ python -c "import pymongo"
Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
ImportError: No module named pymongo
</code></pre>

<p>Other relevant info <code>pip freeze</code> and <code>pip list</code></p>

<pre><code>$ python-pip freeze
backports.ssl-match-hostname==3.4.0.2
ordereddict==1.2
pymongo==3.3.0

$ python-pip list
backports.ssl-match-hostname (3.4.0.2)
ordereddict (1.2)
pip (6.1.1)
pymongo (3.3.0)
setuptools (12.2)
</code></pre>

<p>And loaded paths</p>

<pre><code>$ python
Python 2.7.10 (default, Jul 20 2016, 20:53:27)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys
&gt;&gt;&gt; sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/local/lib64/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/usr/lib64/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
</code></pre>

<p>There are many other questions related to this problem, but none of those have helped. I do not have <code>bson</code> installed and I'm not using any virtual envs.</p>

<p>It turned out that I have two versions of python installed (2.6 and 2.7). Even though default is 2.7, my Ansible script installed <code>pymongo</code> under 2.6 folder <code>/usr/local/lib64/python2.6/site-packages</code></p>

<p>I found that out by running <code>$ python-pip show pymongo</code> and <code>$ python2.6 -c "import pymongo"</code> works as expected.</p>

<p>I changed my Ansible script to install <code>python27-pip</code> instead of <code>python-pip</code> and it started to work nicely.</p>

python ansible pymongo