导入另一个带有选项的PY文件

发布于 2025-02-11 13:59:33 字数 900 浏览 0 评论 0原文

我有main.pynewcanmonitor.py我想用newcanmonitor.py vcan0选项,因为当我使用newcanmonitor.py仅i在python newcanmonitor.py vcan0中键入终端

main.py.py

import newcanmonitor ???  <-- is there any way to use an option 'vcan0' here?

newcanmonitor.py

def read_bus(bus_device):
    """Read data from `bus_device` until the next newline character."""
    message = bus.recv(0.2)
    while True:
        if message:
            break
        message = bus.recv(0.2)

    try:
        string = "{}:ID={}:LEN={}".format("RX", message.arbitration_id, message.dlc)
        for x in range(message.dlc):
            string += ":{:02x}".format(message.data[x])

    except Exception as e:
        print(e)
    return string

提前谢谢你们

I have main.py and newcanmonitor.py I want to import newcanmonitor.py with vcan0 option because when I use newcanmonitor.py alone I type in python newcanmonitor.py vcan0 in the terminal

main.py

import newcanmonitor ???  <-- is there any way to use an option 'vcan0' here?

newcanmonitor.py

def read_bus(bus_device):
    """Read data from `bus_device` until the next newline character."""
    message = bus.recv(0.2)
    while True:
        if message:
            break
        message = bus.recv(0.2)

    try:
        string = "{}:ID={}:LEN={}".format("RX", message.arbitration_id, message.dlc)
        for x in range(message.dlc):
            string += ":{:02x}".format(message.data[x])

    except Exception as e:
        print(e)
    return string

Thank you guys in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

染墨丶若流云 2025-02-18 13:59:33

您可以尝试在导入模块之前使用sys.argv.append(“ vcan0”)

import sys

sys.argv.append("vcan0")
import newcanmonitor

但是我不知道是否会与模块一起使用,因为模块会导入sys,并且它可以在没有“ vcan0”的情况下获得新鲜的sys.argv


但是我宁愿使用,如果__name__ ==“ __ main __”在导入时直接并以不同的方式执行时以不同的方式运行代码。

这样,独立版本可以从sys.argv获得值,然后发送到函数 - read_bus(sys.argv) - 和main.py.py.py可以导入函数并直接运行read_bus(“ vcan0”)

newcanmonitor.py

def read_bus(bus_device):
    """Read data from `bus_device` until the next newline character."""
    message = bus.recv(0.2)
    while True:
        if message:
            break
        message = bus.recv(0.2)

    try:
        string = "{}:ID={}:LEN={}".format("RX", message.arbitration_id, message.dlc)
        for x in range(message.dlc):
            string += ":{:02x}".format(message.data[x])

    except Exception as e:
        print(e)
    return string

if __name__ == "__main__":
    import sys

    read_bus(sys.argv[1])

main.py.py

import newcanmonitor

newcanmonitor.read_bus("vcan0")

You can try to uses sys.argv.append("vcan0") before you import module.

import sys

sys.argv.append("vcan0")
import newcanmonitor

but I don't know if will work with module because module will import sys and it can get fresh sys.argv without "vcan0"


But I would rather use if __name__ == "__main__" to run code in differene way when it is executed directly and in different way when it is imported.

This way standalone version could get value from sys.argv and send to function - read_bus(sys.argv) - and main.py can import function and run directly read_bus("vcan0")

newcanmonitor.py

def read_bus(bus_device):
    """Read data from `bus_device` until the next newline character."""
    message = bus.recv(0.2)
    while True:
        if message:
            break
        message = bus.recv(0.2)

    try:
        string = "{}:ID={}:LEN={}".format("RX", message.arbitration_id, message.dlc)
        for x in range(message.dlc):
            string += ":{:02x}".format(message.data[x])

    except Exception as e:
        print(e)
    return string

if __name__ == "__main__":
    import sys

    read_bus(sys.argv[1])

main.py

import newcanmonitor

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