AttributeError: module 'odbc' has no attribute 'connect' – python with pydev

Question:

I am very new to python and I just can’t seem to find an answer to this error. When I run the code below I get the error

AttributeError: module 'odbc' has no attribute 'connect'

However, the error only shows in eclipse. There’s no problem if I run it via command line. I am running python 3.5. What am I doing wrong?

try:
    import pyodbc
except ImportError:
    import odbc as pyodbc

# Specifying the ODBC driver, server name, database, etc. directly
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=PXLstr,17;DATABASE=Dept_MR;UID=guest;PWD=password')

The suggestion to remove the try…except block did not work for me. Now the actual import is throwing the error as below:

Traceback (most recent call last):
  File "C:UsersaworkspaceTestPyProjectsrchelloworld.py", line 2, in <module>
    import pyodbc
  File "C:UsersaAppDataLocalContinuumAnaconda3Libsite-packagessqlalchemydialectsmssqlpyodbc.py", line 105, in <module>
    from .base import MSExecutionContext, MSDialect, VARBINARY

I do have pyodbc installed and the import and connect works fine with the command line on windows.

thank you

Asked By: afora377

||

Answers:

The problem here is that the pyodbc module is not importing in your try / except block. I would highly recommend not putting import statements in try blocks. First, you would want to make sure you have pyodbc installed (pip install pyodbc), preferably in a virtualenv, then you can do something like this:

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=PXLstr,17;DATABASE=Dept_MR;UID=guest;PWD=password')

cursor = cnxn.cursor()
cursor.execute('SELECT 1')

for row in cursor.fetchall():
    print(row)

If you’re running on Windows (it appears so, given the DRIVER= parameter), take a look at virtualenvwrapper-win for managing Windows Python virtual environments: https://pypi.python.org/pypi/virtualenvwrapper-win

Good luck!

Answered By: FlipperPA

Flipper’s answer helped to establish that the problem was with referencing an incorrect library in External Libraries list in eclipse. After fixing it, the issue was resolved.

Answered By: afora377

What is the name of your python file? If you inadvertently name it as ‘pyodbc.py’, you got that error. Because it tries to import itself instead of the intended pyodbc module.

Answered By: Ehsan

here is the solution!
simply install and use ‘pypyodbc’ instead of ‘pyodbc’!
I have my tested example as below. change your data for SERVER_NAME and DATA_NAME and DRIVER. also put your own records.good luck!

import sys
import pypyodbc as odbc

records = [
    ['x', 'Movie', '2020-01-09', 2020],
    ['y', 'TV Show', None, 2019]
]

DRIVER = 'ODBC Driver 11 for SQL Server'
SERVER_NAME = '(LocalDB)MSSQLLocalDB'
DATABASE_NAME = 'D:ASPNETSHOJA.IRSHOJA.IRAPP_DATADATABASE3.MDF'

conn_string = f"""
    Driver={{{DRIVER}}};
    Server={SERVER_NAME};
    Database={DATABASE_NAME};
    Trust_Connection=yes;
"""

try:
    conn = odbc.connect(conn_string)
except Exception as e:
    print(e)
    print('task is terminated')
    sys.exit()
else:
    cursor = conn.cursor()


insert_statement = """
    INSERT INTO NetflixMovies
    VALUES (?, ?, ?, ?)
"""

try:
    for record in records:
        print(record)
        cursor.execute(insert_statement, record)
except Exception as e:
    cursor.rollback()
    print(e.value)
    print('transaction rolled back')
else:
    print('records inserted successfully')
    cursor.commit()
    cursor.close()
finally:
    if conn.connected == 1:
        print('connection closed')
        conn.close()
Answered By: SEYEDMOHSEN

instead if I would like to import xml into an MDB dataframe? in my case I have many xml files that I should manually import xml to xml, I’m trying to figure out how to import them automatically … if someone can give me a hand I would be grateful

Answered By: nicoz
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.