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)
# ...
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 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
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 have a python test program to connect to oracle installed in my laptop. the python program is working fine when called from python console(IDLE) but the same program gives "ModuleNotFoundError: No module named 'cx_Oracle' " error when run from anaconda-spyder.
Please suggest.
the program is as follows:
import cx_Oracle
conn=cx_Oracle.connect('user/password#localhost/SID')
cur=conn.cursor()
cur.execute('select * from employee')
for line in cur:
print(line)
cur.close()
conn.close()
ModuleNotFoundError: No module named 'cx_Oracle'
You have to install and configure the module in Anaconda's enviroment as this question is answered:
How can I instal cx_Oracle package to Anaconda 3 to use with python 3.5
And then import the correct module 'oracle'
In the Anaconda navigator, select 'Environments', then on the right, change the filter to 'All'. Now you can query for 'cx_oracle', select it and at the bottom, select 'Apply'. Once completed, you should be able to
Issue - On CENTOS 7, I have installed MARIADB 10.x which is working perfectly fine, I tested with DBeaver.
to connect MARIA DB 10.x from PYTHON 2.7, I have installed "MySQL-python 1.2.5" as recommended "https://mariadb.com/blog/how-connect-python-programs-mariadb".
But getting below error while testing, Please help with this.
import mysql.connector as mariadb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
UPDATE
The lib MySQLdb1 is not maintained anymore. The MySQL company maintain a connector project.
Use MySQL Connector for Python as MariaDB database connection.
import MySQLdb as mariadb
conn = mariadb.connect(user='username', passwd='1a2b3c', db='defaultdb')
cursor = conn.cursor()
The lib mentioned on MariaDB docs is the one officialy builded by MySQL. Using this lib you will be able to follow the tutorial.
As #nizam-mohamed mentioned in comments, there is a a fork from MySQLdb1, mysqlclient.