attributeError:模块' nats'没有属性'连接'
https://nats-io.github.io/nats.py/modules。 html
Traceback (most recent call last):
File "c:\Users\lb\nats.py", line 22, in <module>
asyncio.run(main())
File "D:\anaconda\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "D:\anaconda\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "c:\Users\lb\nats.py", line 6, in main
nc = await nats.connect(servers=["nats://216.48.189.5:4222"])
AttributeError: module 'nats' has no attribute 'connect'
属性错误:我无法弄清“连接”的问题。
import asyncio
import nats
async def main():
# Connect to NATS!
nc = await nats.connect(servers=["nats://216.48.189.5:4222"])
# Receive messages on 'foo'
sub = await nc.subscribe("foo")
# Publish a message to 'foo'
await nc.publish("foo", b'Hello from Python!')
# Process a message
msg = await sub.next_msg()
print("Received:", msg)
# Close NATS connection
await nc.close()
if __name__ == '__main__':
asyncio.run(main())
请帮助我解决这个连接问题。
https://nats-io.github.io/nats.py/modules.html
Traceback (most recent call last):
File "c:\Users\lb\nats.py", line 22, in <module>
asyncio.run(main())
File "D:\anaconda\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "D:\anaconda\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "c:\Users\lb\nats.py", line 6, in main
nc = await nats.connect(servers=["nats://216.48.189.5:4222"])
AttributeError: module 'nats' has no attribute 'connect'
An Attribute error: I am not able to figure out what is the issue with 'connect'.
import asyncio
import nats
async def main():
# Connect to NATS!
nc = await nats.connect(servers=["nats://216.48.189.5:4222"])
# Receive messages on 'foo'
sub = await nc.subscribe("foo")
# Publish a message to 'foo'
await nc.publish("foo", b'Hello from Python!')
# Process a message
msg = await sub.next_msg()
print("Received:", msg)
# Close NATS connection
await nc.close()
if __name__ == '__main__':
asyncio.run(main())
Help me with this connect issue, please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是将您的文件重命名为其他任何内容。
键入
导入NATS
Python递归尝试查找名为nats
的文件夹。在这种情况下,第一个文件如果会找到的是nats.py
。您永远不会在
nats.py
中定义名为connect
的函数,这样就会失败。您需要让递归导入继续至site_packages
其中实际nats
文件夹是包含连接函数的文件夹。例如,如果您将此文件命名
Hello.py.py
:您将看到所有3个打印语句打印同一件事,因为
Hello
将是文件本身。简单地重命名您的文件,其他任何内容都会阻止Python过早找到模块,并将继续搜索直到找到正确的模块。
The solution is to rename your file to anything else.
When you type
import nats
python recursively tries to find a file or folder namednats
. In this case the first file if will find isnats.py
.You never define a function named
connect
in yournats.py
so that fails. You want to instead let the recursive import continue uptosite_packages
where the actualnats
folder is that contains the connect function.For example, if you name this file
hello.py
:You will see that all 3 print statements print the same thing, since
hello
will be the file itself.Simply renaming your file anything else will prevent python from finding the module too soon and will keep searching till it find the correct module.
我只是运行了您的代码,没有错误。
尝试卸载并重新安装NAT:
I just ran your code and got no error.
Try uninstalling and reinstalling nats: