Python - AttributeError: object "NoneType" does not have attribute "cursor"

I have the following code:

import pyodbc conn = pyodbc.connect('DSN=QueryBuilder') cursor = conn.cursor() stringA = "SELECT GrantInformation.Call FROM GrantInformation" cursor.execute(stringA) rows = cursor.fetchall() 

For many years it worked perfectly, but suddenly it stopped working today with the following trace:

 Traceback (most recent call last): File "C:/Users/nicholas/Desktop/test.py", line 6, in <module> cursor = conn.cursor() AttributeError: 'NoneType' object has no attribute 'cursor' 

The code still works in the Jupyter Notebook, but no longer works outside ie in Pycharm / IDLE

If I run in Pycharm:

 print(pyodbc.dataSources()) print(pyodbc.drivers()) 

I get:

 none none 

If I do this in a Jupyter Notebook, I get:

 {'Visio Database Samples': 'Microsoft Access Driver (*.mdb, *.accdb)', 'dBASE Files': 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'Excel Files': 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'MS Access Database': 'Microsoft Access Driver (*.mdb, *.accdb)', 'QueryBuilder': 'SQL Server', 'QueryBuilder_Beta': 'SQL Server', 'MIDAS': 'SQL Server', 'Querybuilder_YearEnd': 'SQL Server', 'ResearchFish': 'SQL Server', 'ESRCContacts': 'SQL Server', 'ESRCContactsDEV': 'SQL Server', 'Sample Amazon Redshift DSN': 'Amazon Redshift (x64)'} ['SQL Server', 'SQL Server Native Client 10.0', 'Amazon Redshift (x64)', 'SQL Server Native Client 11.0', 'PostgreSQL ANSI(x64)', 'PostgreSQL Unicode(x64)', 'ODBC Driver 11 for SQL Server', 'ODBC Driver 13 for SQL Server'] 

I am losing plot because I rely a lot on this code to do all my work, but I donโ€™t do anything, this is a fix for the problem, i.e. reinstalling Python, pyodbc, etc.

+5
source share
2 answers

Surprised, nobody noticed this, but it seems that there are problems with Python 3.6 and pyodbc ... I returned to 3.5, and it works fine.

+3
source

conn is None, so you were unable to get the connection. Presumably, in the laptop where this command works, โ€œQueryBuilderโ€ is configured correctly, and everywhere you tried to use it, this does not happen. I am not familiar with this name, but you may need to configure the data source, or you may need to install the appropriate ODBC driver.

You can look at pyodbc.dataSources() and / or pyodbc.drivers() to get more detailed information about which data sources are configured and which drivers are installed, and see what is the difference between your systems.

+4
source

Source: https://habr.com/ru/post/1275186/


All Articles