# Failed to import the required Python library

**URL:** <https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595>\
**Category:** Get Help\
**Tags:** aws, collections\
**Created:** [November 27, 2023, 5:31pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595 "2023-11-27T17:31:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [November 27, 2023, 5:31pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/1 "2023-11-27T17:31:34Z")

</div>

Hello,

I have a problem using the [s3\_object module](https://docs.ansible.com/ansible/latest/collections/amazon/aws/s3_object_module.html). As a requirement, boto3 has to be installed. I’ve been trying to achieve this with the following tasks:

```auto
- name: 'Install pip'
  package:
    name: "pip"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"
  become: true
 
- name: 'Install requirements'
  pip:
    name: "boto3"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"

- ansible.builtin.set_fact:
    ENDPOINT_URL: 'https://this.should.work'
    BACKUP_SCRIPT: 'backup.sh'
  
- ansible.builtin.set_fact:
    BACKUP_SCRIPT_BUCKET: 'my_bucket_name'
    no_log: false

- name: Get backup script from S3 storage
  amazon.aws.s3_object:
    access_key: "{{ aws_access_key_id }} # this exists
    secret_key: "{{ aws_secret_access_key }} # this too
    endpoint_url: "{{ ENDPOINT_URL }}"
    bucket: "{{ BACKUP_SCRIPT_BUCKET }}"
    object: "{{ BACKUP_SCRIPT }}"
    mode: "get"
    dest: "/root"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"

```

Unfortunately, I get:

> “Failed to import the required Python library (botocore and boto3) on ANSIBLE\_HOST’s Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible\_python\_interpreter”

As you can see, I tried to set the right python interpreter on the relevant tasks, but it doesn’t seem to do much. It still can’t find boto3 (and botocore, which is installed as a dependency of boto3).

Python version: 3.10  
Ansible version: 2.12

Any hints are highly appreciated!

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [November 27, 2023, 5:52pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/2 "2023-11-27T17:52:14Z")

</div>

Can you link to a pastebin with the full results of the playbook? Run with `-vvv` or even `-vvvv`? I think one of those should show you the interpreter being used on the remote host, to triple-confirm the python path is correct.

Have you conformed manually that boto3 & botocore were actually installed to the python used by the user that ansible runs under, on the remote host?

---

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [November 27, 2023, 6:21pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/3 "2023-11-27T18:21:20Z")

</div>

hi @jrglynn2  
Here’s the verbose log: [https://pastebin.com/03xsi76w](https://pastebin.com/03xsi76w)

> Have you conformed manually that boto3 & botocore were actually installed to the python used by the user that ansible runs under, on the remote host?

boto3 & botocore were installed using the task I run prior to `amazon.aws.s3_object`. I didn’t use `become: true` so both tasks should run under the same user.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [November 27, 2023, 9:06pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/4 "2023-11-27T21:06:07Z")

</div>

Maybe try the suggestion posted here ==\> [Ansible cannot find boto3/botocore python modules even if they are installed · Issue #41776 · ansible/ansible · GitHub](https://github.com/ansible/ansible/issues/41776#issuecomment-440371161) \<== ?

Install both `boto3` & `botocore`, regardless of the dependency, and with the `--user` argument? So:

```auto
- name: Ensure botocore and boto3 modules are installed
  pip:
    name: ["boto3", "botocore"]
    extra_args: "--user"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"

```

I think it has to do with the Python being used by whatever user Ansible is connecting with…

---

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [November 29, 2023, 10:25am UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/5 "2023-11-29T10:25:28Z")

</div>

Hello again @jrglynn2

I tried it, unfortunately it yields the same error message ☹

My YAML code:

```auto
- name: 'Install pip'
  package:
    name: "pip"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"
  become: true

- name: 'Install requirements'
  pip:
    name: "{{ requirement }}"
    extra_args: "--user"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"
  loop:
    - "botocore"
    - "boto3"
  loop_control:
    loop_var: requirement

- ansible.builtin.set_fact:
    ENDPOINT_URL: 'https://this.should.work'
    BACKUP_SCRIPT: 'backup_v2.sh'

- ansible.builtin.set_fact:
    BACKUP_SCRIPT_BUCKET: 'my_bucket_name'
    no_log: false

- name: Get backup script from S3 storage
  amazon.aws.s3_object:
    access_key: "{{ aws_access_key_id }}"
    secret_key: "{{ aws_secret_access_key }}"
    endpoint_url: "{{ ENDPOINT_URL }}"
    bucket: "{{ BACKUP_SCRIPT_BUCKET }}"
    object: "{{ BACKUP_SCRIPT }}"
    mode: "get"
    dest: "/root"
  vars:
    ansible_python_interpreter: "/usr/bin/python3"

```

---

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [November 29, 2023, 4:01pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/6 "2023-11-29T16:01:13Z")

</div>

@jrglynn2 turned out you were right!

The last task was under a block (which I didn’t want to copy entirely because it was mostely irrelevant to the issue) which is executed with `become: true`. I am so ashamed it took me so long to realize that!

Case closed and thanks for your time! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 29, 2023, 4:02pm UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library/2595/7 "2023-12-29T16:02:01Z")

</div>

Closed on December 29, 2023, 4:02pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
