Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import name 'async_sessionmaker' from 'sqlalchemy.ext.asyncio' in 1.0.0a1 #17022

Closed
2 tasks done
lmocsi opened this issue Jun 17, 2024 · 3 comments · Fixed by #17029
Closed
2 tasks done

Cannot import name 'async_sessionmaker' from 'sqlalchemy.ext.asyncio' in 1.0.0a1 #17022

lmocsi opened this issue Jun 17, 2024 · 3 comments · Fixed by #17029
Assignees
Labels
A-io-database Area: reading/writing to databases bug Something isn't working python Related to Python Polars

Comments

@lmocsi
Copy link

lmocsi commented Jun 17, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

create table my_test nologging pctfree 0 as
select
   'aaa' prod,
   date'2000-01-01' start_date,
   date'9999-12-31' end_date
from dual
import cx_Oracle
import polars as pl

host1 = '***'
host2 = '***'
service = '***'
username = '***'
password = '***'
dsn = f"(DESCRIPTION=(ADDRESS_LIST=(FAILOVER=yes)(ADDRESS=(PROTOCOL=TCP)(HOST={host1})(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST={host2})(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME={service})))"

v_sql = "select * from my_test"

with cx_Oracle.connect(f"{username}/{password}@{dsn}") as con:
    df2 = pl.read_database(
        query=v_sql,
        connection=con
    )

Log output

ImportError                               Traceback (most recent call last)
/tmp/1000780000/ipykernel_1121/1054175297.py in <module>
     13 
     14 with cx_Oracle.connect(f"{username}/{password}@{dsn}") as con:
---> 15     df2 = pl.read_database(
     16         query=v_sql,
     17         connection=con

/opt/conda/envs/Python-3.9-Premium/lib/python3.9/site-packages/polars/io/database/functions.py in read_database(query, connection, iter_batches, batch_size, schema_overrides, infer_schema_length, execute_options)
    251     # return frame from arbitrary connections using the executor abstraction
    252     with ConnectionExecutor(connection) as cx:
--> 253         return cx.execute(
    254             query=query,
    255             options=execute_options,

/opt/conda/envs/Python-3.9-Premium/lib/python3.9/site-packages/polars/io/database/_executor.py in __exit__(self, exc_type, exc_val, exc_tb)
    106         # if we created it and are finished with it, we can
    107         # close the cursor (but NOT the connection)
--> 108         if self._is_alchemy_async(self.cursor):
    109             from sqlalchemy.ext.asyncio import AsyncConnection
    110 

/opt/conda/envs/Python-3.9-Premium/lib/python3.9/site-packages/polars/io/database/_executor.py in _is_alchemy_async(conn)
    319     def _is_alchemy_async(conn: Any) -> bool:
    320         """Check if the cursor/connection/session object is async."""
--> 321         from sqlalchemy.ext.asyncio import (
    322             AsyncConnection,
    323             AsyncSession,

ImportError: cannot import name 'async_sessionmaker' from 'sqlalchemy.ext.asyncio' (/opt/conda/envs/Python-3.9-Premium/lib/python3.9/site-packages/sqlalchemy/ext/asyncio/__init__.py)

Issue description

The above code works fine in polars-0.20.31, fails in polars-1.0.0a1.

Expected behavior

Read the table without errors.

Installed versions

--------Version info---------
Polars:               1.0.0-alpha.1
Index type:           UInt32
Platform:             Linux-4.18.0-372.76.1.el8_6.x86_64-x86_64-with-glibc2.28
Python:               3.9.13 (main, Oct 13 2022, 21:15:33) 
[GCC 11.2.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          2.0.0
connectorx:           0.3.4a1
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               2022.02.0
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           3.8.4
nest_asyncio:         1.5.5
numpy:                1.23.5
openpyxl:             3.0.9
pandas:               2.2.2
pyarrow:              15.0.2
pydantic:             <not installed>
pyiceberg:            <not installed>
sqlalchemy:           1.4.27
torch:                1.10.2
xlsx2csv:             <not installed>
xlsxwriter:           3.2.0

The results are the same with:
connectorx==0.3.3

@lmocsi lmocsi added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jun 17, 2024
@alexander-beedie
Copy link
Collaborator

alexander-beedie commented Jun 17, 2024

Thanks for the report; you seem to have a very old version of sqlalchemy installed (1.4.27 is a little over 2½ years out of date). If you can upgrade it to the latest in the 1.4.x (which would be 1.4.52) the error will resolve. I will harden the async check so it doesn't cause this error, but I wouldn't recommend being on quite such an old version if it can be avoided 👌

@alexander-beedie alexander-beedie removed the needs triage Awaiting prioritization by a maintainer label Jun 17, 2024
@alexander-beedie alexander-beedie added the A-io-database Area: reading/writing to databases label Jun 17, 2024
@alexander-beedie alexander-beedie self-assigned this Jun 17, 2024
@lmocsi
Copy link
Author

lmocsi commented Jun 17, 2024

How about the newest sqlalchemy-2.0.30? It is good, as well?

@hendrix-bates
Copy link

Hi @alexander-beedie
As of 1.0.0 this is still a problem as the hardening was not applied to _is_alchemy_session().
async_sessionmaker is also unforunately not available as late as 1.4.49 of sqlalchemy which is the latest 1.4.x version on conda-forge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-database Area: reading/writing to databases bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants