导入另一个带有选项的PY文件
我有main.py
和newcanmonitor.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试在导入模块之前使用
sys.argv.append(“ vcan0”)
。但是我不知道是否会与模块一起使用,因为模块会导入
sys
,并且它可以在没有“ vcan0”
的情况下获得新鲜的sys.argv
,但是我宁愿使用
,如果__name__ ==“ __ main __”
在导入时直接并以不同的方式执行时以不同的方式运行代码。这样,独立版本可以从
sys.argv
获得值,然后发送到函数 -read_bus(sys.argv)
- 和main.py.py.py
可以导入函数并直接运行read_bus(“ vcan0”)
newcanmonitor.py
main.py.py
You can try to uses
sys.argv.append("vcan0")
before you import module.but I don't know if will work with module because module will import
sys
and it can get freshsys.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)
- andmain.py
can import function and run directlyread_bus("vcan0")
newcanmonitor.py
main.py