Windows: Windows 7 Professional
Python: python-3.6.1-amd64.exe
pyodbc: pyodbc-4.0.16-cp36-cp36m-win_amd64.whl
Eclipse: Neon.3Release(4.6.3) Build id: 20170314-1500
PyDev for Eclipse: 5.7.0201704111357
After I installed pyodbc by run pip install pyodbc-4.0.16-cp36-cp36m-win_amd64.whl, I got unresolved import pyodbc in Eclipse. So I manually added "pyodbc" under Python Interpreter > Forced Builtins and "unresolved import pyodbc" issue solved in Eclipse.
However, auto completion does not work for variable in Eclipse with Pydev. I can get auto completion for pyodbc.connect but not conn.cursor() unless I define conn = pyodbc.Connection.
Auto completion not working for variable conn
import pyodbc
if __name__ == '__main__':
conn = pyodbc.connect('Connecting String....')
cur = conn.Cursor()
Auto completion works for variable conn
import pyodbc
if __name__ == '__main__':
conn = pyodbc.Connection
conn = pyodbc.connect('Connecting String....')
cur = conn.Cursor()
This is mostly because PyDev can't infer what pyodbc.connect returns (it can't really execute that, it can only see that it's a method call and try to guess from its docstring).
You can help it though by adding type hints in docstrings as:
#: :type conn: pyodbc.Connection
See: http://www.pydev.org/manual_adv_type_hints.html for more info
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)
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
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
Eclipse version : Version: 3.8.1
import sqlite3
sqlite3.connect('database.db')
print "Database connected successfully"
in the above code, sqlite3.connect() is displayed as
Undefined variable from import: connect
But when i run the code it is running properly, why is that it is displayed as Error in eclipse-pydev?
I also faced the similar issue, solution is -->
You need to add 'sqlite3' (without the quotatios) in the 'forced builtins' tab in Window>Preferences>PyDev>Python Interpreter
check this link for details
Python: Unresolved import error for sqlite3 in PyDev in Eclipse
It's probably something quite easy but I can't figure out why my script won't work. I'm trying to make a connection with my sqlite3 database but eclipse returns the error: "Undefined variable from import: connect". I'm running python 3.3 in a virtualenv on linux. Thanks for your help!
from urllib.request import urlopen
import datetime
import sqlite3
class Crawler():
def storeContent(self, html, url):
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT .. ", [item, item])
c.commit()
c.close()
It seems like Alex Barcelo resolved this issue here.
What worked for me on Ubuntu was almost the same*:
cd /usr/lib/python2.7/lib-dynload/
sudo ln -s _sqlite3.x86_64-linux-gnu.so _sqlite3.so
After that, I had to reconfigure the Python Interpreter for my PyDev project:
Project Properties -> PyDev-Interpreter/Grammar -> Click here to configure an interpreter not listed, then delete, run auto-config for the python environment you're using, and hit "Apply".
*Replace "python2.7" with the version of python you're using sqlite3 with, and if "_sqlite3.x86_64-linux-gnu.so" is not the right name of the file for your linux system, you can normally search for it using "locate _sqlite3"