Python API import issues

ahabel Member Posts: 9 ✭✭

Hi Solace Community,

I have a strange issue with the Python API - or I might just be dumb right now ;)

I've started with a pretty simple script that connects to a broker and just reports the client id and disconnects. (removed sensitive values...)

from solace.messaging.messaging_service import MessagingService, RetryStrategy
from solace.messaging.config.authentication_strategy import BasicUserNamePassword
from solace.messaging.config.transport_security_strategy import TLS

def main():
    broker_props = {"": "tcps://myhost",
                    "solace.messaging.service.vpn-name": "myvpn"}

    transport_security = TLS.create().without_certificate_validation()

    messaging_service = MessagingService.builder() \
        .from_properties(broker_props) \
        .with_authentication_strategy(BasicUserNamePassword("username", "*****")) \
        .with_connection_retry_strategy(RetryStrategy.forever_retry(10000)) \
        .with_reconnection_retry_strategy(RetryStrategy.forever_retry(10000)) \
        .with_transport_security_strategy(transport_security) \


    if messaging_service.is_connected:

if __name__ == "__main__":

My project structure looks pretty simple (I'm using activated venv):


And when I run this script with my virtualenv enabled I get this:


So the script is working fine, Solace API seems to be installed correctly in my .venv aso...

Now let's move the script to a subfolder like src


When I run python src/ or cd into src and then run python I get

Traceback (most recent call last):
  File "src/", line 1, in <module>
    from solace.messaging.messaging_service import MessagingService, RetryStrategy
ModuleNotFoundError: No module named 'solace.messaging'

I tried out setting PYTHONPATH to different values - but no luck.

which python

find .venv | grep solace/messaging/__init__

I'm using solace_pubsubplus-1.2.1

Sorry, but I'm lost ;)
I'haven't had any issues with other libraries - in fact src/ contains a Django App where I wanted Solace to connect with...



  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 474 admin
    edited January 2022 #2

    Hey @ahabel ! Thanks for raising this issue. I'm curious to know what version of python and pip are you using? I would also recommend upgrading your pip after you have activated your virual env pip install --upgrade pip

    From the explanation of your setup, there should not be a discrepancy in behaviour if you change the location of your python script directory if you have activated you virtual environment (which seems like you have). In fact, this is the same setup we use in our python samples.

    This is how I have my local environment configured

    ## Create virtual environment
    ~/samples/solace-samples-python > python3 -m venv venv
    ## Activate virtual environment
    ~/samples/solace-samples-python > source venv/bin/activate
    ~/samples/solace-samples-python > which python                                                                        py solace-samples-python
    ~/samples/solace-samples-python > python --version                                                                    py solace-samples-python
    Python 3.8.9
    ## Upgrade pip
    ~/samples/solace-samples-python > pip install --upgrade pip                                        py solace-samples-python
    Collecting pip
      Using cached pip-21.3.1-py3-none-any.whl (1.7 MB)
    Installing collected packages: pip
      Attempting uninstall: pip
        Found existing installation: pip 20.2.3
        Uninstalling pip-20.2.3:
          Successfully uninstalled pip-20.2.3
    Successfully installed pip-21.3.1
    ~/samples/solace-samples-python > pip --version                                                                       py solace-samples-python
    pip 21.3.1 from /Users/tamimi/samples/solace-samples-python/venv/lib/python3.8/site-packages/pip (python 3.8)
    ## Install requirements
    ~/samples/solace-samples-python > pip install -r requirements.txt                                  py solace-samples-python
    Collecting certifi==2020.12.5
      Using cached certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
    Collecting chardet==4.0.0
      Using cached chardet-4.0.0-py2.py3-none-any.whl (178 kB)
    Collecting idna==2.10
      Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
    Collecting requests==2.25.1
      Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
    Collecting solace-pubsubplus==1.2.1
      Using cached solace_pubsubplus-1.2.0-py36-none-macosx_10_15_x86_64.whl (5.2 MB)
    Collecting urllib3==1.26.4
      Using cached urllib3-1.26.4-py2.py3-none-any.whl (153 kB)
    Installing collected packages: urllib3, idna, chardet, certifi, solace-pubsubplus, requests
    Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 solace-pubsubplus-1.2.0 urllib3-1.26.4
    ~/samples/solace-samples-python > pip list                                                                            py solace-samples-python
    Package           Version
    ----------------- ---------
    certifi           2020.12.5
    chardet           4.0.0
    idna              2.10
    pip               21.3.1
    requests          2.25.1
    setuptools        49.2.1
    solace-pubsubplus 1.2.1
    urllib3           1.26.4
    ~/samples/solace-samples-python > l                                                                                   py solace-samples-python        howtos           patterns         requirements.txt venv
    ~/samples/solace-samples-python > l patterns                                                                      py solace-samples-python trusted-store
    ## Run Python application
    ~/samples/solace-samples-python > python patterns/                                                py solace-samples-python
    Messaging Service connected? True
    Direct Subscriber is running? True
    Subscribing to: ['solace/samples/python/dir/sub/>', 'solace/samples/python/dir/sub/v2/>', 'solace/samples/>']

    and I am able to run my python script under the patterns directory (which is not in the same level where my venv virtual environment folder is)

    Let me know if this is helpful and if you are still facing the same issue

  • ahabel
    ahabel Member Posts: 9 ✭✭

    Hi @Tamimi,

    thanks for your hints. Finally I solved the issue... :smile:

    TLDR; I've had a folder called solace under src, so the import tried to find the client API in this folder instead of the site-packages in .venv. After renaming the subfolder, everything works fine.