Why is can0 Showing as None After Importing from Testbench File in Multiple Python Scripts?
I am working with Python’s python-can library to interface with a CAN bus. In my project, I want to initialize a global variable (can0) for the CAN interface in a separate testbench file (variable_dec.py), and then use it across multiple other Python files by simply importing variable_dec.py. However, I am encountering an issue where the can0 variable is initialized correctly in one file but shows as None in the other files despite importing the variable_dec file(I am working on raspberry pi with two can channels one sender and the other receiver. programiing with python using VSCode)
In variable_dec.py, I defined the can0 global variable and initialized it using:
python Copy: can0 = can.interface.Bus(channel=’can0′, bustype=’socketcan’) In other Python files, I imported the variable_dec.py file using: from variable_dec import * The issue is : The can0 variable is initialized correctly in one file but always shows as None in the other files. The error message is not being displayed as expected, and the program shows no errors or warnings in those files only the word “None” after running the main program(where all the files using “can0” are excuted)!
Has anyone encountered a similar issue with initializing global variables like can0 in one Python file and importing them into others? What might be causing the None result in files that import variable_dec.py? I would appreciate any suggestions or insights on how to properly share and use the can0 variable across multiple files.Thanks!