No module found named cursors


No module found named cursors



I just installed PyMySQL on my VPS, which did not seem to have any problems installing.



To make sure everything was working fine, I tried executing the following script that I found in their documentation.



Python 2.7.5 and Python 3.6.5


Python 2.7.5


Python 3.6.5



Server version: 10.1.34-MariaDB MariaDB Server


Server version: 10.1.34-MariaDB MariaDB Server


import pymysql.cursors

# Connect to datebase
con = pymysql.connect(host='localhost',
user='dbuser',
password='dbpass',
db='dbname',
charset='utf8mb4',
cursorclass=pymysql.cursor.DictCursor)

try:
with con.cursor() as cursor:
# Create new record
sql = "INSERT INTO `test` (`idx`, `title`, `description`, `category`) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (1, 'Fake Title', 'Fake description about a fake post.', 'WELLNESS'))

with con.cursor() as cursor:
# Read a single record
sql = "SELECT `idx`, `title` FROM `test` WHERE `idx`=%s"
cursor.execute(sql, ('1'))
result = cursor.fetchone()
finally:
with open('dbText.txt', 'w') as f:
f.write('%s'.format(result))
con.close()



However, I am receiving an ImportError that reads No module found named cursors.


ImportError


No module found named cursors


[root@host public_html]# python pymysql.py

Traceback (most recent call last):
File "pymysql.py", line 1, in <module>
import pymysql.cursors
File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ImportError: No module named cursors

[root@host public_html]# python3.6 pymysql.py
Traceback (most recent call last):
File "pymysql.py", line 1, in <module>
import pymysql.cursors
File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ModuleNotFoundError: No module named 'pymysql.cursors'; 'pymysql' is not a package



When I try to reinstall, it indicates that pymysql is already installed.


pymysql


[root@host public_html]# pip install pymysql
Requirement already satisfied: pymysql in /usr/local/lib/python3.6/site-packages (0.9.0)
Requirement already satisfied: cryptography in /usr/local/lib/python3.6/site-packages (from pymysql) (2.2.2)
Requirement already satisfied: idna>=2.1 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (2.7)
Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (1.11.0)
Requirement already satisfied: cffi>=1.7; platform_python_implementation != "PyPy" in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (1.11.5)
Requirement already satisfied: asn1crypto>=0.21.0 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (0.24.0)
Requirement already satisfied: pycparser in /usr/local/lib/python3.6/site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->pymysql) (2.18)



Do I need to install the cursors module separately?


cursors



Any help would be huge!





How did you install it?
– l'L'l
Jun 30 at 3:59





@l'L'l pip install pymysql
– Anthony
Jun 30 at 4:02





Strange, because it should install that module along with it; yours appears to be missing.
– l'L'l
Jun 30 at 4:03






@l'L'l updated my question with much more info
– Anthony
Jun 30 at 4:08





You have this question tagged as Python 2.7 although your pip shows that you’re installing the package for Python 3.6. Are you sure the version of Python you are using is the one you think it is? Try Python -V and report back the version.
– l'L'l
Jun 30 at 5:47



Python -V




1 Answer
1



This part of the stacktrace reveals the issue:


File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ImportError: No module named cursors



You've named the file you're using to test "pymysql.py" -- so, Python tries to grab the cursors module from the selfsame file. If you rename the file to something else, it should be able to load the correct library file, as long as pymysql is installed via pip2/pip2.7 (seems pip on your system defaults to the Python 3 installation).



Additionally, from looking at the the package and documentation itself, I think that when you initialize the MySQL connection object (con), it should use pymysql.cursors.DictCursor (cursors, not cursor) for the cursorclass.


con


pymysql.cursors.DictCursor





omg, i'm crossing my fingers and hoping that this is the issue. such a silly mistake. funny bcuz i just helped somebody on here with a piece of code that had an extremely similar issue lol
– Anthony
Jun 30 at 17:04





It's often the little things that getcha! None of us are immune :)
– Austin Burk
2 days ago







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV