Python code error - Help!

We are using robotpy for our robot code, and when I deploy on the robot using python3 robot.py deploy --nc, the output in the console is AttributeError: module 'subsystems' has no attribute 'DriveTrain'. I am not very good at python and if someone could really quickly proofread my hierarchy coding, that would be amazing! Here’s my code:


roborio.zip (7.6 KB)

For a start, you’re not telling it the name of the file that contains DriveTrain. It doesn’t search through all files found in subsystems/ looking for it, so you need to tell it. Try for example from subsystems.driveTrain import DriveTrain and then instantiate with just self.DriveTrain = DriveTrain(). Another option would be to create an __init__.py file in the subsystems folder, and inside it you could do from .driveTrain import DriveTrain, and then your above code would work.

Also it would probably be good to follow the PEP 8 naming conventions, so for example use self.drive_train instead of self.DriveTrain. Consistently following the conventions will help you learn faster, make it easier for others to read your code or help you, and help save you from other mistakes.

You’ll also quickly find other problems in the DriveTrain code. For example, you are mixing up class and instance variables. The __init__(self) method is assigning a bunch of instance variables, which will be set on “self” and thereby only on the individual instance that you create. But the other methods are not taking the “self” parameter (e.g. it should be def setSpeed(self, speed):), and are referencing the values as though they are class variables, meaning they would need to be attached to the DriveTrain class definition itself, and would therefore be visible on all DriveTrain objects that you create rather than just the one instance. You’ll need to add the self parameter to those other methods (always as the first argument), and then change all the DriveTrain.xxxx stuff into self.xxxx.

1 Like

Once you’ve done the above, you’ll also probably find you need to change your import from import robotMap to from .. import robotMap.

You can learn more about these things here: 6. Modules — Python 3.11.2 documentation

1 Like

thank you guys!

This one doesn’t need to be changed assuming there’s a robotMap.py next to the robot.py.