attributeError:模块' nats'没有属性'连接'

发布于 2025-02-03 13:07:02 字数 1233 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2025-02-10 13:07:02

解决方案是将您的文件重命名为其他任何内容。

键入导入NATS Python递归尝试查找名为nats的文件夹。在这种情况下,第一个文件如果会找到的是nats.py

您永远不会在nats.py中定义名为connect的函数,这样就会失败。您需要让递归导入继续至site_packages其中实际nats文件夹是包含连接函数的文件夹。

例如,如果您将此文件命名Hello.py.py

import hello

print(hello.hello)
print(hello)
print(hello.hello.hello)

您将看到所有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 named nats. In this case the first file if will find is nats.py.

You never define a function named connect in your nats.py so that fails. You want to instead let the recursive import continue upto site_packages where the actual nats folder is that contains the connect function.

For example, if you name this file hello.py:

import hello

print(hello.hello)
print(hello)
print(hello.hello.hello)

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.

注定孤独终老 2025-02-10 13:07:02

我只是运行了您的代码,没有错误。
尝试卸载并重新安装NAT:

pip install nats-py

I just ran your code and got no error.
Try uninstalling and reinstalling nats:

pip install nats-py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文