0

I created a pair of programs for a Raspberry Pi Model B V1.2 running Python 2.7.13. One, a module containing a class and functions and the other a simple example program utilizing the module.

This is the class module, saved as "motor_library.py"

class Motor(object):
    def __init__(self, name, pin1, pin2, pin3, pin4):
        ...
    def callMotor(self, direction, stepNumber, delay):
        ...

This is the example program.

import motor_library

motor1 = Motor('motor1',4,17,23,24)
motor1.callMotor(1,1000,1)

After running the example program I get the error "NameError: name 'Motor' is not defined." Both files are in the same directory and I can't figure out what's causing the error. After some searchin I can't find anyone with a similar issue, anyone able to lend a hand?

Evan
  • 11
  • 3
  • 1
    It should be `motor1 = motor_library.Motor('motor1',4,17,23,24)` or you will have to change your import statement to `from motor_library import Motor` – game0ver Jul 19 '18 at 21:18
  • Possible duplicate of [Import a module from a relative path](https://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path) – m0etaz Jul 19 '18 at 22:25

1 Answers1

2

There are two options:

Import the module which makes the module available in the current namespace when the classes defined in the module can only be accessed as its attributes:

import motor_library

motor1 = motor_library.Motor('motor1',4,17,23,24)

or import the class directly:

from motor_library import Motor

motor1 = Motor('motor1',4,17,23,24)
user2390182
  • 72,016
  • 6
  • 67
  • 89