Python ZMQ functions magically missing? - python

So I have been trying to use the ZMQ module to make a publisher subscriber model
I downloaded the library in Pycharm, made sure that it is the latest version.
And when I try to use things like ".setsockopt" it keeps saying "unresolved attribute reference setsockopt for class "Socket" and ask me to add a method to the class "Socket" This is exteremly annoying and I have no idea how to solve this. I went online to read tutorials and all they did is just "import zmq" and everything is fine. Please help me=(
Its not only setsockopt, I have been trying things like .send_pyobj and it doesn't work too!
Here I literally copy and paste a code from youtube: youtube.com/watch?v=-_CXA8SZsOs
please go to time 5:58, I copy that code and my pycharm say that on line15: socket.send_pyobj has error and say the same thing "unresolved attribute reference setsockopt for class "Socket"
Somebody please help me your help means a lot to me
The youtube code:
import zmq
from time import sleep
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')
messages = [100,200,300]
curMsg = 0
while(True):
sleep(1)
socket.send_pyobj({curMsg:messages[curMsg]})
if (curMsg == 2):
curMsg = 0
else:
curMsg = curMsg +1
I have the following traceback error incase you are wondering
note that it is for rec_obj which is pretty much the same issue with send_obj and also setsockopt. Basically it is missing for no reasons
File "C:\Users\jacky\PycharmProjects\testing_the_send_data_and_receive_data_thing\venv\lib\site-packa
ges\zmq\sugar\attrsettr.py", line 44, in getattr
raise AttributeError(
AttributeError: Socket has no such option: REC_PYOBJ

Related

AttributeError: module 'socket' has no attribute 'setblocking'

I'm trying to use 'non-blocked socket' for a Python project
(see previous question if anyone has a better answer : How to use a socket without waiting in python )
I saw that people on the site suggested using the command:
socket.setblocking ()
But when I run the program it crashes, and the error is recorded:
AttributeError: module 'socket' has no attribute 'setblocking'
How can I fix this?
And is there another way?
It looks like you are trying to call setblocking() on the module called socket rather than on the object called socket.
Change your code to something like:
import socket
s = socket.socket(...)
s.setblocking(...)

"AttributeError: 'module' object has no attribute" - Can someone explain the meaning of this error message?

I'm trying to get the ODB library working. In the documentation at https://python-obd.readthedocs.io/en/latest/ it lists the following code:
import obd
connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port
cmd = obd.commands.SPEED # select an OBD command (sensor)
response = connection.query(cmd) # send the command, and parse the response
print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions
When I put this in a file called test.py and I run it:
python2 test.py
I get the following error message:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import obd
File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'
Stackoverflow comes up with several iterations of this error message, but none clearly explain the problem, only giving specific solutions to those libraries.
I guess it's obvious that I'm new to Python, and I'm having trouble interpreting this error message, even after several hours of writing several small Python programs. I am of course also interested in why the error message is so un-intuitive to a newcomer and where I can gain the common knowledge I might be missing, and I guess this is as good a case to discover that through as any.
So far I have figured out the following:
<module> refers to the name of my python script that I am trying to run.
The mistake I made at first, was to name my test.py file, initially obd.py. This conflicted with the import obd as it was trying to import the file itself - even after I deleted it! The reason was that when I tried to run it the first time, it created a file called obd.pyc - and even though obd.py was no longer there, import OBD found the initially created obd.pyc and tried to import that - which of course does not contain the OBD object from the library I was trying to use.
This is not the detailed answer I'm looking for, so please add a more detailed explanation - or a link to how Python compiling works, if you can.

Serial Communication: python

I am using Python Anaconda 2.7. I would like to toggle the port using serial communication, But I am getting error: AttributeError: 'module' object has no attribute 'Serial'
My sample program is:
import serial
import time
#remember to adjust “COM3”
port = serial.Serial("COM3", 19200, timeout=0.5)
#turn on port 2, sleep 2 seconds, turn off port 2
port.write(b"\nF2\r")
time.sleep(2.0)
port.write(b"\nF2\r")
#turn on port 2 and 7, sleep 2 seconds, turn off port 2 and 7
port.write(b"\nF2\r\nF7\r")
time.sleep(2)
port.write(b"\nF2\r\nF7\r")
#close the port
port.close()
I have tried many solutions:
changing file name from 'serial.py' to 'any_other_name.py' and vise versa
deleting related .pyc file
Installing 'pip install pyserial'
Doing From serial import serial
When I run the same program from psychopy, it is working really fine. I don't know How to solve it. If some one can give me suggestions that It would be great help for me. Thanking you in advance.
Ravi
Your code seems good, so the problem you encounter is probably due to a bad import.
You really should avoid to name your python script like "standard" modules (serial.py, string.py...), because by doing this, you expose yourself to accidentally import those files instead of the correct one (probably what happened to you).
If you need to be sure of what you're importing, try this :
import serial
print serial.__file__ # this will tell you the position of the file you've imported and help you to be sure of what you're using.
# in case you're not importing a module, only a class... try this :
help(serial) # even without any help, it will give you at the end the path where this object is defined :)

About Match Objects in python

I'm fairly new to python and am trying to run the below code:
import re
start([default])
email = 'akshay.bung17#gmail.com'
m = re.search('kit',email)
print m.group()
But i keep getting the following error:
start([default])
NameError: name 'start' is not defined
Can anyone please let me know what exactly is happening over here?
As people have said in the comments, this error message means exactly what it says - you never defined start.
I'd strongly suggest reading this article on how to debug small programs.

How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)

Looking for help/tutorials/sample code of using python to listen to distributed notifications from applications on a mac. I know the py-objc lib is the bridge between python and mac/cocoa classes, and the Foundation library can be used to add observers, but looking for examples or tutorials on how to use this to monitor iTunes.
If anyone comes by to this question, i figured out how to listen, the code below works. However accessing attributes do not seem to work like standard python attribute access.
Update: you do not access attributes as you would in python i.e (.x), the code has been updated below, it now generates a dict called song_details.
Update3: Update to the code, now subclassing NSObject, removed adding the addObserver from the class. Will keep the code updated on github, no more updates here.
import Foundation
from AppKit import *
from PyObjCTools import AppHelper
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
for x in ui:
song_details[x] = ui.objectForKey_(x)
print song_details
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None)
NSLog("Listening for new tunes....")
AppHelper.runConsoleEventLoop()
The source code for GrowlTunes might give you some clues here. You'd have to translate from Objective-C to PyObjC, but eh, whatever. :)
GrowlTurnesController.m
(Or grab the whole growl source tree and navigate to GrowlTunes so you can see it all in action.: here's a link to the directions on how to get the source

Categories

Resources