0

我正在尝试将 MPU6050 模块用于 NodeMCU 中的 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()

“模块”没有方法 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'
4

1 回答 1

0

改变

import myFile.py

import myFile

您正在导入模块 ( myFile),而不是文件。让 Python 解决它。

于 2018-04-11T11:07:27.713 回答