Error while open app after using pyinstaller - python

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)

Related

Getting AttributeError: module 'mysql' has no attribute 'connector' in 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

Cannot access an sqlite3 database file with python

I'm trying to take recipe hyperlinks from a pre-existing file and add them to a table in a database. I have already created the database and given the file name to set up a connection and insert the data into the table but whenever I do so I get the error sqlite3.OperationalError: unable to open database file.
Here's my code:
import bs4, os, requests, time
import sqlite3
from flask import current_app, g
def create_connection(db_file):
db = sqlite3.connect(db_file)
c = db.cursor()
return db
def get_html():
'''
Get the BBC Food sitemap and save it to a local file.
'''
page = None
db = create_connection("pantry/instance/flaskr.sqlite")
for attempt in range(1, 4):
print("line 40")
page = requests.get('http://www.bbc.co.uk/food/sitemap.xml')
try:
page.raise_for_status()
break
except requests.RequestException:
time.sleep(attempt * 10)
if not page:
raise Exception('Failed to get sitemap.xml')
sitemap = bs4.BeautifulSoup(page.text, 'html.parser')
# Write the recipe urls to a text file
print("line 53")
for line in sitemap.find_all('loc'):
for string in line.stripped_strings:
if string.startswith('https://www.bbc.co.uk/food/recipes/'):
print("line 57")
recipeUrl = string
if (
db.execute("SELECT recipeID FROM recipe WHERE weblink = ?", (recipeUrl,)).fetchone()
is not None
):
error = "recipe weblink {0} is already inputted.".format(recipeUrl)
if error is None:
db.execute(
'INSERT INTO recipe (weblink) VALUES (?)',
recipeUrl
)
db.commit()
db.close()
And the error message:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 161, in <module>
get_html()
File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 34, in get_html
db = create_connection("pantry/instance/flaskr.sqlite")
File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 23, in create_connection
db = sqlite3.connect(db_file)
sqlite3.OperationalError: unable to open database file
Based on the consoles error report, your .py file is located in: C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py".
In your code you set your databases directory using the relative path as:pantry/instance/flaskr.sqlite.
This means python is looking for the directory: C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/pantry/instance/ to create/link your database file in.
If that is not a preexisting directory, sqlite3.connect will not be able to create your database flaskr.sqlite.
You may need to update the code db = create_connection("pantry/instance/flaskr.sqlite") to something like db = create_connection("flaskr.sqlite") which should work without any problem since it will just create the database in your working directory.

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)

Python Hive/hadoop connection error

I am trying to use Hive/Hadoop using python.
import pyhs2
with pyhs2.connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='None',
password='None',
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 the following error.
Traceback (most recent call last):
File "pyth_hive2.py", line 8, in <module>
database='Default') as conn:
File "build/bdist.linux-x86_64/egg/pyhs2/__init__.py", line 7, in connect
File "build/bdist.linux-x86_64/egg/pyhs2/connections.py", line 46, in __init__
File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 74, in open
File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 92, in _recv_sasl_message
File "build/bdist.linux-x86_64/egg/thrift/transport/TTransport.py", line 58, in readAll
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 94, in read
thrift.transport.TTransport.TTransportException: None
Can someone tell me how to go about this?

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