Trying to create a bigquery connector using sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('bigquery://<project_id>/<project_name>',
credentials_path=GCP_KEY)
conn = engine.connect()
Error:
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:bigquery
Your error is generally related to the lack of some required module to use SQLAlchemy.
Therefore, after going though the documentation, I found out that you should install the requirements in you environment using:
pip3 install pybigquery
In addition, within your script you should import the following modules:
SQLAchemy
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
API Client
from pybigquery.api import ApiClient
Afterwards, you should have all the necessary packages to execute your code.
If you have any more question about using the SQLAlchemy and API client for BigQuery, you can consult the provided documentation above or I would also be glad to help.
Related
I'm running my Fast API project with pipenv and I use Python 3.6.3 as the interpreter.
For production I use a postgres db which works fine.
Now I want to test my api with pytest and sqlite.
Therefore I changed the db connection setup:
engine = create_engine("sqlite:///./test.db")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
db = SessionLocal()
I also tried the connection string from the official docs from sqlalchemy:
engine = create_engine('sqlite://')
when I run my test , I get the following error:
engine = create_engine("sqlite:///./test.db")
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py:298: in warned
return fn(*args, **kwargs)
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/engine/create.py:560: in create_engine
dbapi = dialect_cls.dbapi(**dbapi_args)
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py:473: in dbapi
from sqlite3 import dbapi2 as sqlite
../../../../.pyenv/versions/3.6.3/lib/python3.6/sqlite3/__init__.py:23: in <module>
from sqlite3.dbapi2 import *
../../../../.pyenv/versions/3.6.3/lib/python3.6/sqlite3/dbapi2.py:27: in <module>
from _sqlite3 import *
E ModuleNotFoundError: No module named '_sqlite3'
I have seen in the source code that the error causing module has the following note:
pysqlite2/dbapi2.py: the DB-API 2.0 interface
#
# Copyright (C) 2004-2005 Gerhard Häring <gh#ghaering.de>
#
# This file is part of pysqlite.
Here the error is cause by
from _sqlite3 import *
where _sqlite is not found.
I had a look at the
dbapi2.py
file from pysqlite3. Here sqlite is imported without the "_".
So I thought since python3 the pysqlite3 file should be inbuilt in python.
Is there a way to change the pysqlite2 to 3? I've read about installing pysqlite3, but this did not change anything in my case.
I also tried to update from python 3.6.13 to 3.6.3 as 3.6 is mandatory. This also did not change the behaviour.
I was able to solve it by the information provided by #MatsLindh
Here is the original comment with the solution.
I have installed
sudo apt-get install libsqlite3-dev
and then reinstalled my python version with pyenv
I want to import a dataframe into a access database but I got an error NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:access.pyodbc
from sqlalchemy import create_engine
import urllib
import pyodbc
conec = (r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
r"DBQ=C:\Users\redim\Desktop\18_marzo\Libr2.accdb"
)
con = f"access+pyodbc:///?odbc_connect={urllib.parse.quote_plus(conec)}"
acc_engine = create_engine(con)
df.to_sql('hola', acc_engine)
For some reason this only works in Jupyter notebooks, not in PyCharm which I was having the same problems for quite some time in both until I upgraded SQLalchemy
I use conda install with most of my libraries:
conda update sqlalchemy
But if you are using pip, then below is command:
pip install --upgrade sqlalchemy
I went from sqlalchemy 1.3.7 to 1.4.39 and the error went away, at least in Jupyter Notebooks anyways.
I am trying to connect to the sybase database using sqlalchemy. I am doing my development in Linux environment. I am also using python 2.7
I have the following code in which I am just trying to connect to the database. When I try run the piece of code with just the import part, I do not get the error ImportError: No module named pyodbc.
I only get the error with the import when I uncomment and run the sal.create_engine part of the code. I am not able to understand why it is saying the it is unable to import the pyodbc.
My code as of now:
import sqlalchemy as sal
from sqlalchemy import create_engine
from sqlalchemy.connectors import pyodbc
# in order to connect, we need server name, database name we want to connect to
engine = sal.create_engine('sybase+pyodbc://dialect+driver://username:password#host:port/database', echo = True)
conn = engine.connect()
# printing names of the tables present in the database
#print(engine.table_names())
May someone help me to resolve this issue?
I am using Python 2.7.5
I installed flask-mysql and so on... I already have a few database stored proc working but i am having some issues with flask_mysql
anytime i run my code it it says:
api.py:5: ExtDeprecationWarning: Importing flask.ext.mysql is deprecated, use flask_mysql instead.
from flask.ext.mysql import MySQL
C:\Python27\lib\site-packages\flask\exthook.py:106: ExtDeprecationWarning: Detected extension named flaskext.mysql,
please rename it to flask_mysql. The old form is deprecated.
.format(x=modname), ExtDeprecationWarning
here is how i imported flask_mysql
from flask.ext.mysql import MySQL
my question is how do i import flask_mysql the right way or is their any other better alternative i could use to replace it
Your question already has an answer:
Importing flask.ext.mysql is deprecated, use flask_mysql instead.
So, as it says: replace from flask.ext.mysql import MySQL with from flask_mysql import MySQL.
I'm trying to get Flask to connect to a local MySQL but this keeps failing. I have tried multiple combinations based on other forums
from flask.ext.mysql import MySQL
from flask_mysql import MySQL
from flaskext.mysql import MySQL
etc.
the error I get is the following
ImportError: No module named flask.ext.mysql
Running Windows 10 & Python3.5
I have pip installed flask-MySQL
********** EDIT ***********
With the help of the user #metmirr (Thank you!!!) the fix was to use a virtual environment, couldn't get it to work any other way! The answer and comments have been removed somehow
Use mysql.connector for accessing MySQL from Flask. For more details visit the link.
Import MySQL Connector
import mysql.connector
Connect to local database
def getMysqlConnection():
return mysql.connector.connect(host='localhost',database='test',user='root',password='root')
To access the database
db = getMysqlConnection()
sqlstr = "select * from test_table"
cur = db.cursor()
cur.execute(sqlstr)
output_json = cur.fetchall()