How to use psycopg2 module using Mac - python

I am trying to import psycopg2 in a python file I created to establish a connection with my Postgres database. Issue is that my system shows I have psycopg2 installed when I run the following in my terminal pip freeze | grep psycopg2 but in my .py file I have the following code
import psycopg2
conn = psycopg2.connect('dbname=test user=postgres')
cur = conn.cursor()
cur.execute("SELECT * FROM my_data")
records = cur.fetchall()
and it fails at the import. The problem I get is Import "psycopg2" count not be resolved from source Pylance(reportMissingModuleSource)
What am I doing wrong. I have psycopg2-binary installed as well. My Mac version is Big Sur version 11.6
My psycopg2 version is 2.8.6

Related

Python 3.6.3 unable to import _sqlite3 to run a SQlite db with sqlalchemy

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

NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:access.pyodbc

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.

Unable to connect to Remote Oracle Database using Python script

I am unable to connect to the remote Oracle DB using python script. I am writing the below code to my script, and facing the error as below:
I have checked the following checkpoints as below:
1. Version of Python and Oracle client is same i.e. 64 bit
2. cx_Oracle module installation
3. I have downloaded ODPI-C but I don't know what to do with it.
I am using MAC OSX.
Code:
import sys
import cx_Oracle
dsn_tns=cx_Oracle.makedsn('dbpx87mp.co.zing.com','1901','bpx87mp')
conn=cx_Oracle.connect('admin','Password',dsn_tns)
cur=conn.cursor()
cur.execute("select * from <table_name>")
for line in cur:
print(line)
cur.close()
conn.close()
Error:
Traceback (most recent call last):
File "/Users/558220/Library/Preferences/PyCharmCE2019.2/scratches/oraScript.py", line 6, in <module>
conn=cx_Oracle.connect('admin','Password',dsn_tns)
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "dlopen(libclntsh.dylib, 1): image not found". See https://oracle.github.io/odpi/doc/installation.html#macos for help
On macOS, you can't use the default system Python with cx_Oracle because this Python has restricted entitlements and will fail to load Oracle client libraries.
Use Python via something like homebrew or install Python yourself. See the cx_Oracle installation instructions https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#installing-cx-oracle-on-macos

ModuleNotFoundError - PyMySQL for python 3

I am trying to get a simple test program working on my machine that connects to a SQL DB. I pip installed and then uninstalled and then installed with pip3: pymysql. the issue I'm getting:
import PyMySQL
ModuleNotFoundError: No module named 'PyMySQL'.
it can be found when i run the command pip list, but not found when running the program itself. I was perusing over other SO Q&A's but nothing helped.
Thanks
First insall PyMySQL using:
pip install PyMySQL
In python 3 it is pymysql, all in lower case
import pymysql
Module names are case-sensitive, and if you take a look at this example in the GitHub repo, you'll see that the module should be imported as pymysql, which is consistent with the all-lowercase convention for modules spelled out in the PEP 8 style guide.
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# ...

Trying to use Flask-mysql with python

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()

Categories

Resources