Since the optuna documentation does not address which modules are required from MySQL, I installed everything of MySQL on my Windows 10 machine. I looked for MySQL on my PC (in which folder the installation takes place is not revealed during installation) and updated the Path variables to
C:\Program Files\MySQL\MySQL Server 8.0\bin
I have successfully created the mysqltestexample database.
Using python SQL connectors, I can reproduce the output using:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Start123"
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Start123",
database="mysqltesteexample"
)
Connection to the mysqltesteexample does not raise an error - so everything seems to be fine. However, optuna is not able to connect to my database
My python script looks like this. It is the code from the optuna documentation, I just altered the name of the test database.
study0 = optuna.create_study(storage="mysql://root#localhost/mysqltesteexample",study_name="distributed-example")
study0 = optuna.create_study(storage="mysql+pymysql://root:Start123#localhost:3306/mysqltesteexample",study_name="distributed-example")
All attempts to modify the URL string according to https://docs.sqlalchemy.org/en/14/core/engines.html failed with the following error: ImportError: Failed to import DB access module for the specified storage URL. Please install appropriate one.
Can you please help me to get it done? Thank you in advance, please don't be too harsh.
Finally, I made it. I have to install some further packages from the cmd:
py -3.8 -m easy_install mysql-python
py -3.8 -m pip install mysqlclient
Python packages - as well documented as they are eyes rolling
Related
I installed pyodbc with pip a couple of weeks ago, all my other scripts work and connect to SQL Server perfectly fine but for some reason this script won't run... I've uninstalled and reinstalled it still giving same error
python code
import pyodbc
connector = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};SERVER=localhost;UID=SA;PWD=Working#2022;DATABASE=testdb;Encrypt=no;TrustServerCertificate=yes")
cursor = connector.cursor()
cursor.execute("USE testdb")
cursor.execute("SELECT * FROM overtime_forecast")
for i in cursor:
print(i)
I am trying to connect my python code to sql server. But there is an error which shows:
import pyodbc
ModuleNotFoundError: No module named 'pyodbc'
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server};"
"Server=localhost;"
"Database=SCMS2;"
"uid=sa;pwd=tazbirul94")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Suppliers')
for row in cursor:
print('row = %r' % (row,))
Here is the pip freeze command:
This shows my odbc is present:
Here is the error
Here is my pycharm environment path
The pycharm is using a python from a .../venv/... folder while what you type in the cmd does not. Make sure to use the same python version for both.
One way would be to go to:
File->Settings->Project->Project Interpreter and change to the python that you have installed the module for
I am using Jupyter Notebooks to learn Python. I would like to connect to a MySQL db hosted locally hosted through MAMP. How would I approach this?
import os
import pymysql
import pandas as pd
host = os.getenv('MYSQL_HOST')
port = os.getenv('MYSQL_PORT')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
database = os.getenv('MYSQL_DATABASE')
conn = pymysql.connect(
host=host,
port=int(3306),
user="root",
passwd=password,
db="[YOUR_DB_NAME]",
charset='utf8mb4')
df = pd.read_sql_query("SELECT * FROM YOUR_TABLE",
conn)
df.tail(10)
Assuming you have MySQL installed (instructions here for macOS using HomeBrew), you need to:
Install pip3 install ipython-sql
pip3 install mysqlclient
now you should be able to run these cells and get pretty-printed HTML output:
# %%
%load_ext sql
# %%
%sql mysql+mysqldb://<user>:<password>#localhost/<dataBase>
# %%
%%sql
SELECT *
FROM <table>;
import pymysql
import pandas as a
conn=pymysql.connect(host='localhost',port=int(3306),user='root',passwd='YOUR_PASSWORD',db='YOUR_DATABASENAME')
df=a.read_sql_query("SELECT * FROM 'YOUR_TABLENAME' ",conn)
print(df)
Yes, you can. You can use the MySQL Connector library. Simply install it using pip, and then you can use it to interact with your database. See the sample code below:
import mysql.connector
db = mysql.connector.connect(
host="localhost",
user="mamp",
passwd=""
)
print(db)
I'm trying to use the Google App Engine's dev server with MySQL and sqlalchemy on my local machine.
For some reason, I can connect to my MySQL server just fine in an interactive session, but the same connection code fails when run from the dev server.
I have a virtualenv with sqlalchemy installed, and I've symlinked the virtualenv's site-packages directory into a directory that the dev server can see [a].
I also have mysql-connector installed system-wide, and I have symlinked the system wide install into my virtualenv's site-packages.
Here's an interactive session showing that the libs are set up properly (assemblies is the root directory of my GAE project):
>>> import sqlalchemy as sa
>>> import sqlalchemy.orm as orm
>>> import assemblies.models as models
>>> engine = sa.create_engine('mysql+mysqlconnector://<username>:<password>#localhost/test')
>>> S = orm.sessionmaker(bind=engine)
>>> session = S()
>>> session.query(models.User).all()
[<assemblies.models.User object at 0x0000000003043978>, <assemblies.models.User
object at 0x0000000003043F28>, <assemblies.models.User object at 0x0000000003043
F98>]
However, when I run almost the same code in my GAE program, I get a connection error from the MySQL server.
Here's the main app file:
import sqlalchemy as sa
import sqlalchemy.orm as orm
engine = sa.create_engine(<db url>, echo=True)
Session = orm.sessionmaker(bind=engine)
import models as models
from google.appengine.api import users
<unrelated JINJA stuff etc.>
class Users(webapp2.RequestHandler):
def get(self):
session = Session()
users = session.query(models.User).all()
This raises a connection error:
File "D:\gaetest\assemblies\main.py", line 34, in get
users = session.query(models.User).all()
File "D:\gaetest\assemblies\lib\sqlalchemy\orm\query.py", line 2398, in all
return list(self)
...
File "D:\gaetest\assemblies\lib\mysql\connector\network.py", line 251, in recv_plain
errno=2055, values=(self.get_address(), _strioerror(err)))
OperationalError: (mysql.connector.errors.OperationalError) 2055: Lost connectio n to MySQL server at 'localhost:3306', system error:
I checked the database url and made sure I activated the virtualenv.
What could be causing this connection problem?
Does GAE need additional configuration for this to work?
[a]: I essentially use the official instructions on "vendoring" but where the vendor directory (called lib in the instructions) is a symlink to the virtualenv's site-packages. This lets the virtualenv see the libs when I'm working independently of the GAE dev server.
The problem was this
I also have mysql-connector installed system-wide, and I have symlinked the system wide install into my virtualenv's site-packages.
GAE doesn't support mysql-connector. It only supports mysql-python which uses MySQLdb.
Therefore, you have to install mysql-python.
Installing it system-wide and using the symlink trick mentioned in OP works fine if you want to do that.
I found below link
http://python.projects.pgfoundry.org/docs/1.0/driver.html#connection-keywords
it says...
import postgresql.driver as pg_driver
is the way to import
i used -
import postgresql.driver as pg_driver
pg_driver.connect(user = self.username, password = self.password, host = self.host, port = self.port,sslmode = 'verify-full', sslrootcert=self.ssl_cert)
but it gives 'ImportError: No module named postgresql.driver'
i tried 'pip install postgresql'
but it gives 'Could not find any downloads that satisfy the requirement postgresql
No distributions at all found for postgresql'
how can i fixed that ???
You can download latest version of py-postgresql and use 'Python34\python.exe setup.py install' to install it.
You can check Python34\Lib\site-packages\ if there is a folder called postgresql which proves it's installed correctly. Then try import postgresql.driver as pg_driver again and tell me if it works.