diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 2d24a5d7..f261539d 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -13,7 +13,7 @@ from ..crypto import rsa from ..extensions import markdown from ..network import MTProtoSender, ConnectionTcpFull from ..network.mtprotostate import MTProtoState -from ..sessions import Session, SQLiteSession +from ..sessions import Session, SQLiteSession, MemorySession from ..tl import TLObject, functions, types from ..tl.alltlobjects import LAYER @@ -171,7 +171,18 @@ class TelegramBaseClient(abc.ABC): # Determine what session object we have if isinstance(session, str) or session is None: - session = SQLiteSession(session) + try: + session = SQLiteSession(session) + except ValueError: + import warnings + warnings.warn( + 'The sqlite3 module is not available under this ' + 'Python installation and no custom session ' + 'instance was given; using MemorySession.\n' + 'You will need to re-login every time unless ' + 'you use another session storage' + ) + session = MemorySession() elif not isinstance(session, Session): raise TypeError( 'The given session must be a str or a Session instance.' diff --git a/telethon/sessions/sqlite.py b/telethon/sessions/sqlite.py index 7d81f00c..b13a850c 100644 --- a/telethon/sessions/sqlite.py +++ b/telethon/sessions/sqlite.py @@ -1,7 +1,6 @@ import datetime import json import os -import sqlite3 from base64 import b64decode from os.path import isfile as file_exists @@ -13,6 +12,11 @@ from ..tl.types import ( InputPhoto, InputDocument, PeerUser, PeerChat, PeerChannel ) +try: + import sqlite3 +except ImportError: + sqlite3 = None + EXTENSION = '.session' CURRENT_VERSION = 4 # database version @@ -27,6 +31,9 @@ class SQLiteSession(MemorySession): """ def __init__(self, session_id=None): + if sqlite3 is None: + raise ValueError('sqlite3 is not installed') + super().__init__() self.filename = ':memory:' self.save_entities = True