在MicroPython

时间:2018-04-11 07:40:16

标签: nodemcu micropython

我正在尝试在NodeMCU中使用MPU6050模块进行MicroPython,使用uPyCraft作为编辑器。 (https://github.com/larsks/py-mpu6050/blob/master/mpu6050.py

我遇到了一些麻烦,我试图简化代码,我只是尝试调用在不同文件中定义的类,我无法让它工作。这就是我到目前为止所做的:

我创建了一个新文件(myFile.py),它看起来像:

import machine

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

然后在我的main.py中我做了:

import myFile

myclass = myFile.myClass()

我运行main.py并在执行myclass = myFile.myClass()行时出现此错误:

  

'module'没有方法myClass

我没有使用MicroPython的经验(虽然我是C#编码器),所以我很确定有一些关于我缺少的语法的细节。有什么帮助吗?

这里有一些测试(文件名是真实的,我在问题中简化了):

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

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

其他:

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

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

然后在跑步时:

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'

1 个答案:

答案 0 :(得分:0)

更改

import myFile.py

import myFile

您正在导入模块(myFile),而不是文件。让Python对其进行排序。