<p dir="auto"><strong>Describe the bug</strong><br>
When executing a query using mssql+pyodbc driver, the following error occurs if using SQLAlchemy version 1.4.16 or newer with pyodbc 4.0.31:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')"><pre class="notranslate"><code class="notranslate">sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
</code></pre></div>
<p dir="auto"><strong>To Reproduce</strong></p>
<p dir="auto">Install dependencies pyodbc (tested on latest, 4.0.31) and SQLAlchemy 1.4.16 or newer.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sqlalchemy import create_engine

connection_string = &quot;mssql+pyodbc://localhost/master?driver=ODBC+Driver+17+for+SQL+Server?Trusted_Connection=yes&quot;

engine = create_engine(connection_string)
engine.execute(&quot;select 1 as result&quot;)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span> <span class="pl-k">import</span> <span class="pl-s1">create_engine</span>

<span class="pl-s1">connection_string</span> <span class="pl-c1">=</span> <span class="pl-s">"mssql+pyodbc://localhost/master?driver=ODBC+Driver+17+for+SQL+Server?Trusted_Connection=yes"</span>

<span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-en">create_engine</span>(<span class="pl-s1">connection_string</span>)
<span class="pl-s1">engine</span>.<span class="pl-en">execute</span>(<span class="pl-s">"select 1 as result"</span>)</pre></div>
<p dir="auto"><strong>Error</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3212, in _wrap_pool_connect
    return fn()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 301, in connect
    return _ConnectionFairy._checkout(self)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 761, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 419, in checkout
    rec = pool._do_get()
  File &quot;...\sqlalchemy\pool\impl.py&quot;, line 145, in _do_get
    self._dec_overflow()
  File &quot;...\sqlalchemy\util\langhelpers.py&quot;, line 70, in __exit__
    compat.raise_(
  File &quot;...\sqlalchemy\util\compat.py&quot;, line 207, in raise_
    raise exception
  File &quot;...\sqlalchemy\pool\impl.py&quot;, line 142, in _do_get
    return self._create_connection()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 247, in _create_connection
    return _ConnectionRecord(self)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 362, in __init__
    self.__connect()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 605, in __connect
    pool.logger.debug(&quot;Error on connect(): %s&quot;, e)
  File &quot;...\sqlalchemy\util\langhelpers.py&quot;, line 70, in __exit__
    compat.raise_(
  File &quot;...\sqlalchemy\util\compat.py&quot;, line 207, in raise_
    raise exception
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 599, in __connect
    connection = pool._invoke_creator(self)
  File &quot;...\sqlalchemy\engine\create.py&quot;, line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File &quot;...\sqlalchemy\engine\default.py&quot;, line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File &quot;...\example.py&quot;, line 6, in &lt;module&gt;
    engine.execute(&quot;select 1 as result&quot;)
  File &quot;&lt;string&gt;&quot;, line 2, in execute
  File &quot;...\sqlalchemy\util\deprecations.py&quot;, line 390, in warned
    return fn(*args, **kwargs)
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3107, in execute
    connection = self.connect(close_with_result=True)
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3166, in connect
    return self._connection_cls(self, close_with_result=close_with_result)
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 96, in __init__
    else engine.raw_connection()
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3245, in raw_connection
    return self._wrap_pool_connect(self.pool.connect, _connection)
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3215, in _wrap_pool_connect
    Connection._handle_dbapi_exception_noconnection(
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 2068, in _handle_dbapi_exception_noconnection
    util.raise_(
  File &quot;...\sqlalchemy\util\compat.py&quot;, line 207, in raise_
    raise exception
  File &quot;...\sqlalchemy\engine\base.py&quot;, line 3212, in _wrap_pool_connect
    return fn()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 301, in connect
    return _ConnectionFairy._checkout(self)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 761, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 419, in checkout
    rec = pool._do_get()
  File &quot;...\sqlalchemy\pool\impl.py&quot;, line 145, in _do_get
    self._dec_overflow()
  File &quot;...\sqlalchemy\util\langhelpers.py&quot;, line 70, in __exit__
    compat.raise_(
  File &quot;...\sqlalchemy\util\compat.py&quot;, line 207, in raise_
    raise exception
  File &quot;...\sqlalchemy\pool\impl.py&quot;, line 142, in _do_get
    return self._create_connection()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 247, in _create_connection
    return _ConnectionRecord(self)
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 362, in __init__
    self.__connect()
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 605, in __connect
    pool.logger.debug(&quot;Error on connect(): %s&quot;, e)
  File &quot;...\sqlalchemy\util\langhelpers.py&quot;, line 70, in __exit__
    compat.raise_(
  File &quot;...\sqlalchemy\util\compat.py&quot;, line 207, in raise_
    raise exception
  File &quot;...\sqlalchemy\pool\base.py&quot;, line 599, in __connect
    connection = pool._invoke_creator(self)
  File &quot;...\sqlalchemy\engine\create.py&quot;, line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File &quot;...\sqlalchemy\engine\default.py&quot;, line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/14/rvf5)"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "...\sqlalchemy\engine\base.py", line 3212, in _wrap_pool_connect
    return fn()
  File "...\sqlalchemy\pool\base.py", line 301, in connect
    return _ConnectionFairy._checkout(self)
  File "...\sqlalchemy\pool\base.py", line 761, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "...\sqlalchemy\pool\base.py", line 419, in checkout
    rec = pool._do_get()
  File "...\sqlalchemy\pool\impl.py", line 145, in _do_get
    self._dec_overflow()
  File "...\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "...\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "...\sqlalchemy\pool\impl.py", line 142, in _do_get
    return self._create_connection()
  File "...\sqlalchemy\pool\base.py", line 247, in _create_connection
    return _ConnectionRecord(self)
  File "...\sqlalchemy\pool\base.py", line 362, in __init__
    self.__connect()
  File "...\sqlalchemy\pool\base.py", line 605, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "...\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "...\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "...\sqlalchemy\pool\base.py", line 599, in __connect
    connection = pool._invoke_creator(self)
  File "...\sqlalchemy\engine\create.py", line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File "...\sqlalchemy\engine\default.py", line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\example.py", line 6, in &lt;module&gt;
    engine.execute("select 1 as result")
  File "&lt;string&gt;", line 2, in execute
  File "...\sqlalchemy\util\deprecations.py", line 390, in warned
    return fn(*args, **kwargs)
  File "...\sqlalchemy\engine\base.py", line 3107, in execute
    connection = self.connect(close_with_result=True)
  File "...\sqlalchemy\engine\base.py", line 3166, in connect
    return self._connection_cls(self, close_with_result=close_with_result)
  File "...\sqlalchemy\engine\base.py", line 96, in __init__
    else engine.raw_connection()
  File "...\sqlalchemy\engine\base.py", line 3245, in raw_connection
    return self._wrap_pool_connect(self.pool.connect, _connection)
  File "...\sqlalchemy\engine\base.py", line 3215, in _wrap_pool_connect
    Connection._handle_dbapi_exception_noconnection(
  File "...\sqlalchemy\engine\base.py", line 2068, in _handle_dbapi_exception_noconnection
    util.raise_(
  File "...\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "...\sqlalchemy\engine\base.py", line 3212, in _wrap_pool_connect
    return fn()
  File "...\sqlalchemy\pool\base.py", line 301, in connect
    return _ConnectionFairy._checkout(self)
  File "...\sqlalchemy\pool\base.py", line 761, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "...\sqlalchemy\pool\base.py", line 419, in checkout
    rec = pool._do_get()
  File "...\sqlalchemy\pool\impl.py", line 145, in _do_get
    self._dec_overflow()
  File "...\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "...\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "...\sqlalchemy\pool\impl.py", line 142, in _do_get
    return self._create_connection()
  File "...\sqlalchemy\pool\base.py", line 247, in _create_connection
    return _ConnectionRecord(self)
  File "...\sqlalchemy\pool\base.py", line 362, in __init__
    self.__connect()
  File "...\sqlalchemy\pool\base.py", line 605, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "...\sqlalchemy\util\langhelpers.py", line 70, in __exit__
    compat.raise_(
  File "...\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "...\sqlalchemy\pool\base.py", line 599, in __connect
    connection = pool._invoke_creator(self)
  File "...\sqlalchemy\engine\create.py", line 578, in connect
    return dialect.connect(*cargs, **cparams)
  File "...\sqlalchemy\engine\default.py", line 584, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/14/rvf5)
</code></pre></div>
<p dir="auto"><strong>Versions.</strong></p>
<ul dir="auto">
<li>OS: Windows 10</li>
<li>Python: 3.9.4</li>
<li>SQLAlchemy: 1.4.16+</li>
<li>Database: SQL Server 2017</li>
<li>DBAPI: pyodbc</li>
</ul>
<p dir="auto"><strong>Additional context</strong></p>
<p dir="auto">The link <a href="http://sqlalche.me/e/14/rvf5" rel="nofollow">http://sqlalche.me/e/14/rvf5</a> points out that this is a problem in the DBAPI driver, and that page suggests it is not a SQL Alchemy error. However, the problem is resolved by simply downgrading SQLAlchemy to 1.4.15 and keeping the same pyodbc driver. Perhaps the connection string is not being passed into pyodbc correctly.</p>
<p dir="auto">Using 1.4.20 when I try to make a connection to sql server I get a data source name not found and no default driver specified</p>
<div class="highlight highlight-source-powershell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDr..."><pre class="notranslate">sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) (<span class="pl-s"><span class="pl-pds">'</span>IM002<span class="pl-pds">'</span></span><span class="pl-k">,</span> <span class="pl-s"><span class="pl-pds">'</span>[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDr...</span></pre></div>
<p dir="auto">here is the way I trigger the error</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def test_db_connect(db_cnxn_str):
    engine = sa.create_engine(db_cnxn_str)
    conn = engine.connect() # &lt;&lt; ERROR HAPPENS HERE
    # rs = conn.execute(&quot;SELECT GETDATE()&quot;)
    assert conn"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">test_db_connect</span>(<span class="pl-s1">db_cnxn_str</span>):
    <span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-en">create_engine</span>(<span class="pl-s1">db_cnxn_str</span>)
    <span class="pl-s1">conn</span> <span class="pl-c1">=</span> <span class="pl-s1">engine</span>.<span class="pl-en">connect</span>() <span class="pl-c"># &lt;&lt; ERROR HAPPENS HERE</span>
    <span class="pl-c"># rs = conn.execute("SELECT GETDATE()")</span>
    <span class="pl-k">assert</span> <span class="pl-s1">conn</span></pre></div>
<p dir="auto">If I change my sqlalchemy version to 1.4.12 I am able to connect without any issue.</p>
<p dir="auto"><strong>Versions.</strong></p>
<ul dir="auto">
<li>OS: Windows 10</li>
<li>Python: 3.9</li>
<li>SQLAlchemy: 1.4.20</li>
<li>Database: MSSQL</li>
<li>DBAPI: pyodbc</li>
</ul>