🎄 Happy Holidays! 🥳
Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.
Happy Holidays!
Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.
Python API import issues
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 = {"solace.messaging.transport.host": "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) \ .build() messaging_service.connect() if messaging_service.is_connected: print(messaging_service.get_application_id()) messaging_service.disconnect() if __name__ == "__main__": main()
My project structure looks pretty simple (I'm using activated venv):
.venv receive_events.py
And when I run this script with my virtualenv enabled I get this:
app_8a54949a-e460-4e0b-bce0-354743572c53
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
.venv src/ receive_events.py
When I run python src/receive_events.py
or cd into src
and then run python receive_events.py
I get
Traceback (most recent call last): File "src/receive_messages.py", 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 .venv/bin/python find .venv | grep solace/messaging/__init__ .venv/lib/python3.8/site-packages/solace/messaging/__init__.py
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...
Comments
-
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 /Users/tamimi/samples/solace-samples-python/venv/bin/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 README.md howtos patterns requirements.txt venv ~/samples/solace-samples-python > l patterns py solace-samples-python TLS_connection.py direct_subscriber.py guaranteed_subscriber.py trusted-store direct_publisher.py guaranteed_publisher.py hello_world_pubsub.py ## Run Python application ~/samples/solace-samples-python > python patterns/direct_subscriber.py 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
0