2

I am newbie in case of python, i am using python 3.6 and mysql connecter from mysql website

pip install --allow-external mysql-connector-python mysql-connector-python

Everything was going good. I tried 3 examples from mysql website Create DB and table , insert, select.

But after 2nd example it stops working and giving a error

Traceback (most recent call last):
  File "select.py", line 3, in <module>
    import mysql.connector
  File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\__init__.py", line 37, in <module>
    from .connection import MySQLConnection
  File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\connection.py", line 45, in <module>
    from .network import MySQLUnixSocket, MySQLTCPSocket
  File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\network.py", line 28, in <module>
    import socket
  File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\socket.py", li
ne 52, in <module>
    import os, sys, io, selectors
  File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\selectors.py",
 line 11, in <module>
    import select
  File "C:\Users\preet\Desktop\ptyhon-newspaper\select.py", line 7, in <module>
    cnx = mysql.connector.connect(user='root', password='Jinqm21k',
AttributeError: module 'mysql' has no attribute 'connector'

Example code

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector



cnx = mysql.connector.connect(user='root', password='****',
                              host='localhost',
                              database='worpress')
cursor = cnx.cursor()                             

query = ("SELECT first_name, last_name, hire_date FROM employees "
         "WHERE hire_date BETWEEN %s AND %s")

hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31)

cursor.execute(query, (hire_start, hire_end))

for (first_name, last_name, hire_date) in cursor:
  print("{}, {} was hired on {:%d %b %Y}".format(
    last_name, first_name, hire_date))


cursor.close()  
cnx.close()

All examples stops working including create, insert, select

I unable to figure out what is wrong with it

4
  • Name your script something other than select.py. You're conflicting with a library module name. Commented Jan 7, 2018 at 6:30
  • @john not working after changing name, no luck Commented Jan 7, 2018 at 6:52
  • Do you still have select.pyc? If so, remove it. Commented Jan 8, 2018 at 4:08
  • @JohnGordon , it start working itself, i don't know how, may be a problem in the system Commented Jan 9, 2018 at 5:12

1 Answer 1

2

Your issue was that mysql library was trying to import some packages which imports "select" package which was shadowed by your module "select.py". Please give an updated traceback what is not working now after renaming "select.py".

I also recommend you not to write code in the .py file itself, unless you want this code to be executed on module import. Create a routine instead and call it at __main__ section.

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector

def print_employees():
    cnx = mysql.connector.connect(user='root', password='Jinqm21k',
                              host='localhost',
                              database='worpress')
    cursor = cnx.cursor()
    ...

if __name__ = '__main__':
    print_employees()
Sign up to request clarification or add additional context in comments.

2 Comments

same trackback regardless of file name, same with test.py
Could you please provide new traceback?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.