# mysql\_db and pymysql error

**URL:** <https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [April 8, 2021, 6:03pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943 "2021-04-08T18:03:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 8, 2021, 6:03pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/1 "2021-04-08T18:03:13Z")

</div>

I am trying to move to python 3.6 on centos 7.8 and ansible 2.8.12  
Mysql\_db module fails importing connections from PyMySQL pip module.  
At least with recent versions ( 0.9 ,1.0.2 ) using 0.7 pass.

the error seems to be related to a PEP recommendation E402 .

As per PyMySQL doc , python3 is supported .

here is my error:

{  
“module\_stdout”: “”,  
“module\_stderr”: “Traceback (most recent call last):\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 114, in \n \_ansiballz\_main()\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 106, in \_ansiballz\_main\n invoke\_module(zipped\_mod, temp\_path, ANSIBALLZ\_PARAMS)\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 49, in invoke\_module\n imp.load\_module(‘ **main** ’, mod, module, MOD\_DESC)\n File "/tmp/ansible\_mysql\_db\_payload\_3i8jCj/ **main**.py", line 110, in \n File "/tmp/ansible\_mysql\_db\_payload\_3i8jCj/ansible\_mysql\_db\_payload.zip/ansible/module\_utils/mysql.py", line 33, in \n File "/opt/venvs/py3\_ansible28/lib/python3.6/site-packages/pymysql/ **init**.py", line 59, in \n from . import connections # noqa: E402\n File "/opt/venvs/py3\_ansible28/lib/python3.6/site-packages/pymysql/connections.py", line 167\n \*,\n ^\nSyntaxError: invalid syntax\n”,  
“exception”: “Traceback (most recent call last):\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 114, in \n \_ansiballz\_main()\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 106, in \_ansiballz\_main\n invoke\_module(zipped\_mod, temp\_path, ANSIBALLZ\_PARAMS)\n File "/var/lib/awx/.ansible/tmp/ansible-tmp-1617903869.1782537-29212-149280496397207/AnsiballZ\_mysql\_db.py", line 49, in invoke\_module\n imp.load\_module(‘ **main** ’, mod, module, MOD\_DESC)\n File "/tmp/ansible\_mysql\_db\_payload\_3i8jCj/ **main**.py", line 110, in \n File "/tmp/ansible\_mysql\_db\_payload\_3i8jCj/ansible\_mysql\_db\_payload.zip/ansible/module\_utils/mysql.py", line 33, in \n File "/opt/venvs/py3\_ansible28/lib/python3.6/site-packages/pymysql/ **init**.py", line 59, in \n from . import connections # noqa: E402\n File "/opt/venvs/py3\_ansible28/lib/python3.6/site-packages/pymysql/connections.py", line 167\n \*,\n ^\nSyntaxError: invalid syntax\n”,  
“msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”,  
“rc”: 1,  
“\_ansible\_no\_log”: false,  
“changed”: false  
}

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 8, 2021, 6:10pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/2 "2021-04-08T18:10:21Z")

</div>

If I had to guess, you are executing the module and subsequently pymysql with python2. The paths seem to indicate a python3 virtualenv, but the error indicates that you are executing Python2, with potentially a PYTHONPATH pointing to the Python3 venv.

The error is in regards to the “keyword only marker” defined at:

[https://github.com/PyMySQL/PyMySQL/blob/6ccbecc1a0dfd04065b081950d2d35b1dac0aaa8/pymysql/connections.py#L164-L166](https://github.com/PyMySQL/PyMySQL/blob/6ccbecc1a0dfd04065b081950d2d35b1dac0aaa8/pymysql/connections.py#L164-L166)

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 8, 2021, 6:23pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/3 "2021-04-08T18:23:46Z")

</div>

Oh No… I want to get rid of python2 for my ansible playbooks ☹ been fighting this for weeks …

from ansible itself it seems to be running on python3 :

ansible-playbook 2.8.12

config file = /var/lib/awx/tmp/…/project/ansible.cfg

configured module search path = [‘/var/lib/awx/tmp/…/project/library’]

ansible python module location = /opt/venvs/py3\_ansible28/lib/python3.6/site-packages/ansible

executable location = /opt/venvs/py3\_ansible28/bin/ansible-playbook

python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Is that the KW marker ‘\*’ that make you find I run on python2 ? Is that not being used on python3 ?  
Now I don’t really see how to make good use of python3 for ansible if the underlying system keeps using python2 , is there any good guideline I could use ?  
thanks

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 8, 2021, 6:58pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/4 "2021-04-08T18:58:38Z")

</div>

If you want a module to run with python3, you will generally need to set `ansible_python_interpreter` for that host to get it to use the interpreter you want. We default and fallback to py2.

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 8, 2021, 7:07pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/5 "2021-04-08T19:07:43Z")

</div>

hmm ok but this runs on the controller ( awx ) , so I followed this doc :  
[https://docs.ansible.com/ansible/latest/reference\_appendices/python\_3\_support.html#on-the-controller-side](https://docs.ansible.com/ansible/latest/reference_appendices/python_3_support.html#on-the-controller-side)

Do I need to use the interpreter var in that case ?

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 8, 2021, 11:11pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/6 "2021-04-08T23:11:30Z")

</div>

Now, I tried that in my ansible.cfg and indeed it passed my errors ,  
thank you Matt !  
I am still a bit confused though , Why do we bother creating virtual environments if eventually ansible keeps trying to use a system-wide installed python ?  
really , why is that ?

thanks for enlightenment

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 13, 2021, 8:18pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/7 "2021-04-13T20:18:10Z")

</div>

still doesn’t make sense to me, or doc is not clear

My controller Tower/AWX :  
python3 installed on system  
virtual environment to run ansible installed as per the doc : python3 -m pip install ansible  
Yet, I see playbooks using python3 , but what is that usr/bin/python ?

Why is ansible still using python2 please ?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 13, 2021, 8:22pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/8 "2021-04-13T20:22:31Z")

</div>

The Python interpreter that you use to install ansible, is not necessarily the python used to execute modules, even when targeting localhost.

The controller code, and module code execute completely different, with different rules.

If you don’t have `localhost` in your inventory, you can use something called implicit localhost, as described at [https://docs.ansible.com/ansible/latest/inventory/implicit\_localhost.html](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html)

But you must target that localhost explicitly in your play, or use `delegate_to: localhost` for that to work.

If you have `localhost` in your inventory, then you have to explicitly set the python interpreter to be used for that host, via `ansible_python_interpreter`

See also: [https://docs.ansible.com/ansible/latest/reference\_appendices/faq.html#how-do-i-handle-not-having-a-python-interpreter-at-usr-bin-python-on-a-remote-machine](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-handle-not-having-a-python-interpreter-at-usr-bin-python-on-a-remote-machine)

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [April 13, 2021, 9:17pm UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/9 "2021-04-13T21:17:40Z")

</div>

ok thanks, sorry but my tower/awx is not remote per say as it is the box that executes all playbooks , so it like my localhost sure .  
the link describe how to set the interpreter on remote hosts, which in my case are fine using their python2 ( legacy boxes) .  
But if I define the interpreter on tower ansible.cfg (localhost) in default section then even remote hosts inherits that config .  
I d like to only run python3 on the controller (localhost) itself if that s doable .

---

<div class="post-metadata">

**Author:** ![Komal\_Suthar](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@Komal\_Suthar](https://forum.ansible.com/u/Komal_Suthar)\
**Post date:** [April 14, 2021, 12:59am UTC](https://forum.ansible.com/t/mysql-db-and-pymysql-error/34943/10 "2021-04-14T00:59:18Z")

</div>

If you want to install the different version first install the python3 interpreter using yum then it bydefault comes with pip3 command so run pip3 install ansible.
