Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python 3: TypeError: Can't instantiate abstract class BaseConnection with abstract method create_connection

I am upgrading some very old Python code from Python 2 to Python 3. There is a simple method to check rabbitmq connection using Pika.

    from contextlib import closing
    from pika import URLParameters, BaseConnection

    def check_rabbitmq(self):
        conn_params = URLParameters(self.config.rabbitmq_params['amqp.url'])
        with closing(BaseConnection(conn_params)):
            return True

However, in Python 3 it returns

TypeError: Can't instantiate abstract class BaseConnection with abstract method create_connection I feel like I’m missing something obvious.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As per the error, Can't instantiate abstract class BaseConnection with abstract method create_connection. For that what we can do is to use one of its concrete subclasses for establishing a connection, such as BlockingConnection. using that below is the modified chunk of code:

def check_rabbitmq(self):
    conn_params = pika.URLParameters(self.config.rabbitmq_params['amqp.url'])
    with pika.BlockingConnection(conn_params) as connection:
        return True

Make sure you also import pika at the top.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading