0

I'm trying to use the MPU6050 module for MicroPython in NodeMCU, using uPyCraft as an editor. (https://github.com/larsks/py-mpu6050/blob/master/mpu6050.py)

I've got some troubles and I tried to simplify the code to the point I just try to call a class defined in a different file and I cannot get it to work. This is what I've done so far:

I created a new file (myFile.py) and it looks like:

import machine

class myClass(object):
    def __init__(self):
        print("init method called")

Then in my main.py I do:

import myFile

myclass = myFile.myClass()

I run main.py and I get this error when doing myclass = myFile.myClass() line:

'module' has no method myClass

I have no experience with MicroPython (although I'm a C# coder) so I'm pretty sure there is some detail about the syntax I'm missing. Any help?


here some tests (file name is the real one, I simplified in the question):

>>> print(open('mpuController.py').read())
import machine

class myClass(object):
    def __init__(self):
        print("init method called")

other:

>>> print (open('testMPU.py','r').read())
import time
import mpuController

print("starting")
mpu = mpuController.myClass()
print("finished")

and then when running:

exec(open('./testMPU.py').read(),globals())
starting
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 5, in <module>
AttributeError: 'module' object has no attribute 'myClass'
javirs
  • 1,049
  • 26
  • 52
  • I don't think it's a syntax problem. As I mentioned on [github](https://github.com/larsks/py-mpu6050/issues/1), you need to provide details about how your files are getting onto your nodemcu board. In the REPL, what does `print(open('myFile.py').read()` output? Does it show the expected content for that file? – larsks Apr 12 '18 at 12:12
  • added those prints from REPL. I also tried with `class myClass:` without the `(object)`. Nothing changed. I´ve been using uPyCraft for uploading and running. But one can see the REPL exe(... command so ... shouldnt really mater.. isnt it ? – javirs Apr 12 '18 at 16:25
  • I tried adding the class to the same file and instantiating (no import needed) it works nice... I really don't get it :) – javirs Apr 12 '18 at 16:28

1 Answers1

0

Change

import myFile.py

to

import myFile

You're importing a module (myFile), not a file. Let Python sort it out.

dda
  • 6,030
  • 2
  • 25
  • 34
  • sorry, checked my code. This is exactly what I was doing. Edited my question to reflect it. Thanks for the error spotting :) – javirs Apr 11 '18 at 12:08