# Getting 'open\_url' cannot import error

**URL:** <https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834>\
**Category:** Get Help\
**Tags:** galaxy-ng\
**Created:** [November 11, 2024, 4:45pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834 "2024-11-11T16:45:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 11, 2024, 4:45pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/1 "2024-11-11T16:45:21Z")

</div>

I have a python (v3.12.x) installation of Ansible, ansible-core, ansible-galaxy, etc. and I am unable to make any calls to ‘open\_url’ or ‘get\_url’ either directly or indirectly through another task that may call these two utilities.

ansible [core 2.17.6]  
config file = /home/greggl/.ansible.cfg  
configured module search path = [‘/home/greggl/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /home/greggl/.pyenv/versions/3.12.4/lib/python3.12/site-packages/ansible  
ansible collection location = /home/greggl/.ansible/collections/ansible\_collections  
executable location = /home/greggl/.pyenv/versions/3.12.4/bin/ansible  
python version = 3.12.4 (main, Oct 15 2024, 15:27:28) [GCC 13.2.0] (/home/greggl/.pyenv/versions/3.12.4/bin/python3)  
jinja version = 3.1.4  
libyaml = True

---

<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 11, 2024, 4:50pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/2 "2024-11-11T16:50:39Z")

</div>

You have to show your ansible code and the errors you receive, plus results of basic troubleshooting (example - trying these commands from the command line without ansible), or else no one will be able to assist you…

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 11, 2024, 5:42pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/3 "2024-11-11T17:42:08Z")

</div>

OK. My error is not stemming from any thing I am coding, other than calling our a task that makes use of ‘get\_url’ or ‘open\_url’. This same error crops up when I can a ‘yum’ task, a ‘dnf’ task, a ‘get\_url’ direct task, ANY task that reaches out to the ‘url’ Python module.

Here is my one Ansible task that draws the error:

- name: Install Prerequisite Packages  
ansible.builtin.package:  
name:  
- curl  
- wget  
- ca-certificates  
- nfs-utils  
- python3-pip  
- postgresql-contrib  
- postgresql  
- lvm2  
state: present  
environment: “{{ proxy\_env }}”

And the resulting error is:

TASK [infra.prerequisites.install : Install Prerequisite Packages] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: cannot import name ‘open\_url’ from partially initialized module ‘ansible.module\_utils.urls’ (most likely due to a circular import) (/tmp/ansible\_ansible.legacy.dnf\_payload\_ak5cc1qa/ansible\_ansible.legacy.dnf\_payload.zip/ansible/module\_utils/urls.py)  
fatal: [x.x.x.x]: FAILED! =\> {“changed”: false, “module\_stderr”: “Shared connection to [x.x.x.x] closed.\r\n”, “module\_stdout”: “Traceback (most recent call last):\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ\_dnf.py", line 107, in \r\n \_ansiballz\_main()\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ\_dnf.py", line 99, in \_ansiballz\_main\r\n invoke\_module(zipped\_mod, temp\_path, ANSIBALLZ\_PARAMS)\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ\_dnf.py", line 47, in invoke\_module\r\n runpy.run\_module(mod\_name=‘ansible.modules.dnf’, init\_globals=dict(\_module\_fqn=‘ansible.modules.dnf’, \_modlib\_path=modlib\_path),\r\n File "", line 226, in run\_module\r\n File "", line 98, in \_run\_module\_code\r\n File "", line 88, in \_run\_code\r\n File "/tmp/ansible\_ansible.legacy.dnf\_payload\_ak5cc1qa/ansible\_ansible.legacy.dnf\_payload.zip/ansible/modules/dnf.py", line 404, in \r\n File "/tmp/ansible\_ansible.legacy.dnf\_payload\_ak5cc1qa/ansible\_ansible.legacy.dnf\_payload.zip/ansible/module\_utils/urls.py", line 18, in \r\nImportError: cannot import name ‘open\_url’ from partially initialized module ‘ansible.module\_utils.urls’ (most likely due to a circular import) (/tmp/ansible\_ansible.legacy.dnf\_payload\_ak5cc1qa/ansible\_ansible.legacy.dnf\_payload.zip/ansible/module\_utils/urls.py)\r\n”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 11, 2024, 6:53pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/4 "2024-11-11T18:53:10Z")

</div>

It “seems” to be a generic error related to Python installed Ansible versus RHEL RPM ansible installation.

I’ve searched over and over via Google, and not one direct article relates to this issue I am seeing.

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 4:07pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/5 "2024-11-14T16:07:21Z")

</div>

Anyone have any clues on this? I am using Ubuntu 24.04 with Python3.12 installed Ansible.

One thing that I have seen to fix this issue is to apt-get install ansible INSTEAD of python install ansible. For some reason, the python-installed Ansible is hitting this error.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 14, 2024, 4:20pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/6 "2024-11-14T16:20:47Z")

</div>

By _“Python installed Ansible”_ do you mean Ansible was installed using `pip3` or `pipx` or something else?

If you have Ansible installed in your users `$HOME` directory how are you running it to ensure that this version is used rather a system packaged version of Ansible, eg is it in `~/.local/bin` and does that directory come before others in your `$PATH`?

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 4:36pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/7 "2024-11-14T16:36:04Z")

</div>

$ pyenv local 3.12.4  
$ which python3  
/home/greggl/.pyenv/shims/python3  
$ which ansible  
/home/greggl/.pyenv/shims/ansible  
$ ansible --version  
ansible [core 2.17.6]  
config file = /home/greggl/.ansible.cfg  
configured module search path = [‘/home/greggl/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]  
ansible python module location = /home/greggl/.pyenv/versions/3.12.4/lib/python3.12/site-packages/ansible  
ansible collection location = /home/greggl/.ansible/collections/ansible\_collections  
executable location = /home/greggl/.pyenv/versions/3.12.4/bin/ansible  
python version = 3.12.4 (main, Oct 15 2024, 15:27:28) [GCC 13.2.0] (/home/greggl/.pyenv/versions/3.12.4/bin/python3)  
jinja version = 3.1.4  
libyaml = True

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 4:36pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/8 "2024-11-14T16:36:44Z")

</div>

Could this error be from one of those paths in my ansible.cfg?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 14, 2024, 7:27pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/9 "2024-11-14T19:27:42Z")

</div>

That all looks fine, perhaps uninstall the system Ansible to be sure that the issue isn’t caused by different versions conflicting? However this isn’t an issue I’ve had — all the Ansible controllers I use have a Debian Ansible and a `pipx` installed Ansible in my `$HOME`… (I use the Debian Ansible to install the latest Ansible using `pipx`).

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 9:44pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/10 "2024-11-14T21:44:51Z")

</div>

It turns out this Ubuntu image doesn’t have system ansible installed. I think reverting back to RPM-based Ansible installations is what fixes this issue, if I can remember correctly.

I prefer the pip3 installation of Ansible over the system installed Ansible, but I can’t seem to get around using tasks that ultimately hit the get\_url or open\_url.

Very frustrating. And apparently no one else on the Internets has seen this issue before.

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 9:57pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/11 "2024-11-14T21:57:57Z")

</div>

So I bit the bullet and apt installed Ansible-core and Ansible, to go along with my pip3 installed Ansible and ran my playbook and got the same “cannot import name ‘open\_url’ from partially initialized modules 'ansible.module\_utils.urls”

I am starting to believe this is just a pathing issue of some sort. The module is there since ansible-core brings along all of the ansible.builtin.\* tasks, but which pathing is limiting this from being found?

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [November 14, 2024, 10:02pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/12 "2024-11-14T22:02:52Z")

</div>

I’m sitting in $HOME/.ansible and can find the urls.py module at ./plugins/module\_utils/urls.py. What do I need to set/change to tell Ansible to look here too?

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [December 16, 2024, 11:13pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/13 "2024-12-16T23:13:10Z")

</div>

Any one else have a clue here for me? This is now prevalent across any play/role that attempts to pull a URL: agt, dnf, yum, etc. all exhibit this error and I’m blocked now. My only workaround is to revert my tasks away from using these ansible.builtin.\* tasks and simply use shell commands to manually pull a URL as needed – not the best practices solution though. Surely someone has run in to this error before me.

---

<div class="post-metadata">

**Author:** ![glowrimore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/glowrimore/32/5407_2.png) [@glowrimore](https://forum.ansible.com/u/glowrimore)\
**Post date:** [December 16, 2024, 11:42pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/14 "2024-12-16T23:42:29Z")

</div>

I believe I just resolved this issue!

My ~/.ansible.cfg file setup my default module\_utils path as such:

DEFAULT\_MODULE\_UTILS\_PATH(/home/greggl/.ansible.cfg) = [‘/home/greggl/.ansible/plugins/module\_utils’,‘/home/greggl/.pyenv/versions/3.12.4/lib/python3.12/site-packages/ansible/module\_utils’, ‘/usr/local/lib/python3.12/site-packages/ansible/module\_utils’]

Removing the ‘/home/greggl/.ansible/module\_utils’ element from DEFAULT\_MODULE\_UTILS\_PATH in ~/.ansible.cfg eliminated the circular dependency!

---

<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:** [January 15, 2025, 11:43pm UTC](https://forum.ansible.com/t/getting-open-url-cannot-import-error/10834/15 "2025-01-15T23:43:00Z")

</div>

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