Getting AttributeError: module 'mysql' has no attribute 'connector' in python - python

I am on windows 10 and using python3.9.
I installed the pagkage
>python3 -m pip install mysql-connector-python
Now I try to run a simple program
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="pythonDB"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FORM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
I am getting the following error when running
>python3 select.py
Traceback (most recent call last):
File "G:\Python_w3school\mysql\select.py", line 1, in
import mysql.connector
File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector_init_.py", line 42, in
import dns.resolver
File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\dns\resolver.py", line 20, in
import socket
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\socket.py", line 54, in
import os, sys, io, selectors
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\selectors.py", line 12, in
import select
File "G:\Python_w3school\mysql\select.py", line 3, in
mydb = mysql.connector.connect(
AttributeError: module 'mysql' has no attribute 'connector'

try these:
AttributeError: module 'mysql' has no attribute 'connector'
Attribute error:module 'mysql' has no attribute 'connector'
basically change the import to
from mysql import connector

Seems like the Python3 is not taking SQL directly inside the execute function.
Assign the query to a variable and pass it to the execute() function.
It worked for me!!!
sql = "SELECT * FROM customers"
mycursor.execute(sql)
Also try try renaming the file if it is something like select.py to select_customers.py

Related

Teradataml error mlengine_alias_definitions_v1.0' is not defined for the current Vantage version 'vantage1.0'

i'm trying to create an exe file which take advantage of teradataml python. I'm trying to create a table in teradata and import the data form pandas dataframe.
here is my code.
import pandas as pd
from sqlalchemy import create_engine
from teradataml.context.context import *
from sqlalchemy import *
from teradataml.dataframe.copy_to import copy_to_sql
from sqlalchemy.dialects import registry
from teradatasqlalchemy import dialect
registry.register('teradata', 'teradatasqlalchemy', 'dialect')
user = 'dbc'
pasw=user
host = '192.168.1.7'
td_engine = create_engine('teradata://'+ user +':' + pasw + '#'+ host )
create_context(tdsqlengine =td_engine)
df = pd.read_csv(r"C:/krishna/data/FL_insurance_sample1.csv", delimiter=',')
copy_to_sql(df = df, table_name = "Insurece_sample", primary_index="InsurenceID", if_exists="replace")
remove_context()
initially i was getting below error however i fixed that one.
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:teradata
pyinstaller command which i tried:
pyinstaller --add-binary "C:\Users\krishna\AppData\Local\Programs\Python\Python38\Lib\site-packages\teradatasql\teradatasql.dll;teradatasql"-F pyinstalletest.py
the error which i'm getting now:
Traceback (most recent call last):
File "pyinstalletest.py", line 18, in <module>
File "teradataml\context\context.py", line 459, in create_context
File "teradataml\context\context.py", line 751, in _load_function_aliases
File "teradataml\common\utils.py", line 1591, in _check_alias_config_file_exists
teradataml.common.exceptions.TeradataMlException: [Teradata][teradataml](TDML_2069) Alias config file 'C:\Users\krishna\AppData\Local\Temp\_MEI63962\teradataml\config\mlengine_alias_definitions_v1.0' is not defined for the current Vantage version 'vantage1.0'. Please add the config file.
[1660] Failed to execute script pyinstalletest
please help me to resolve the error.

Why does mysql.connector.connect fail with internal error?

I'm upgrading a database application from Python 2.7 to 3.4. I originally used mysqldb, but I'm trying to switch to mysql-connector. My database connection fails with an internal TypeError. Here's the code and the error:
import mysql.connector
try:
dbh = mysql.connector.connect("localhost","pyuser","pypwd","jukebox")
except mysql.connector.Error as err:
print("Failed opening database: {}".format(err))
exit(1)
and here's the error:
# python loadcd.py
Traceback (most recent call last):
File "loadcd.py", line 12, in <module>
dbh = mysql.connector.connect("127.0.0.1","pyuser","pypwd","jukebox")
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 5 were given
I'm at a loss. The exact same connection works with mysqldb, and I can connect with the same credentials using PHP or at the command line.
You should provide keyword arguments:
dbh = mysql.connector.connect(host="localhost",user="pyuser",password="pypwd",database="jukebox")
Use this and replace the values as per your configuration:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="yourusername",
password="yourpassword")
print(mydb)

Unable to connect to Hive2 using Python

While connecting to Hive2 using Python with below code:
import pyhs2
with pyhs2.connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='root',
password='test',
database='default') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from table")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
I am getting below error:
File
"C:\Users\vinbhask\AppData\Roaming\Python\Python36\site-packages\pyhs2-0.6.0-py3.6.egg\pyhs2\connections.py",
line 7, in <module>
from cloudera.thrift_sasl import TSaslClientTransport ModuleNotFoundError: No module named 'cloudera'
Have tried here and here but issue wasn't resolved.
Here is the packages installed till now:
bitarray0.8.1,certifi2017.7.27.1,chardet3.0.4,cm-api16.0.0,cx-Oracle6.0.1,future0.16.0,idna2.6,impyla0.14.0,JayDeBeApi1.1.1,JPype10.6.2,ply3.10,pure-sasl0.4.0,PyHive0.4.0,pyhs20.6.0,pyodbc4.0.17,requests2.18.4,sasl0.2.1,six1.10.0,teradata15.10.0.21,thrift0.10.0,thrift-sasl0.2.1,thriftpy0.3.9,urllib31.22
Error while using Impyla:
Traceback (most recent call last):
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\Scripts\HiveConnTester4.py", line 1, in <module>
from impala.dbapi import connect
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\dbapi.py", line 28, in <module>
import impala.hiveserver2 as hs2
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\hiveserver2.py", line 33, in <module>
from impala._thrift_api import (
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\_thrift_api.py", line 74, in <module>
include_dirs=[thrift_dir])
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\thriftpy\parser\__init__.py", line 30, in load
include_dir=include_dir)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\thriftpy\parser\parser.py", line 496, in parse
url_scheme))
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'
thrift_sasl.py is trying cStringIO which is no longer available in Python 3.0. Try with python 2 ?
You may need to install an unreleased version of thrift_sasl. Try:
pip install git+https://github.com/cloudera/thrift_sasl
If you're comfortable learning PySpark, then you just need to setup the hive.metastore.uris property to point at the Hive Metastore address, and you're ready to go.
The easiest way to do that would be to export the hive-site.xml from the your cluster, then pass --files hive-site.xml during spark-submit.
(I haven't tried running standalone Pyspark, so YMMV)

Error while open app after using pyinstaller

I'm getting error while open my app after using pyinstaller before using pyinstaller when the app still in .py it's working 100%
after that I'm getting this error
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\MySQLdb\__init__.py"
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\_mysql.py", line 7,
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\_mysql.py", line 3,
ImportError: No module named pkg_resources
what I'm doing is :
#!/usr/bin/python
import MySQLdb
from time import sleep
db = MySQLdb.connect(host="localhost", user="root", port=3306, passwd="123456", db="test")
cur = db.cursor()
cur.execute("SHOW TABLES")
for row in cur.fetchall() :
print row
sleep(10)

MySQLdb can't use cursorclass

I am trying to execute the following code:
import MySQLdb
import MySQLdb.cursors
conn=MySQLdb.connect(host = '127.0.0.1',
user = 'root',
passwd = 'root',
db = 'test',
cursorclass = MySQLdb.cursors.DictCursor)
cursor=conn.cursor()
But it gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 243, in cursor
AttributeError: 'Connection' object has no attribute 'cursorclass'
Why is this?
import MySQLdb
import MySQLdb.cursors
conn=MySQLdb.connect(host = '127.0.0.1',
user = 'root',
passwd = 'root',
db = 'test',)
cursor=conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
This is a rough hack, but it may not be advisable to use it. The script with throw out dictionary object by default but the application might require an tuple or array.

Categories

Resources