【发布时间】:2021-06-01 17:14:21
【问题描述】:
我写了一个简单的类“Test.py”。
class Test():
def testMethod():
print('Test method is executed')
我想从命令提示符执行类的方法。
我正在使用以下命令
python -c "from Test import testMethod; testMethod()"
我收到以下错误
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name 'testMethod' from 'Test'
你能帮我找出命令中的问题吗
【问题讨论】:
-
您需要确保您的
Test类可以在命令行执行级别找到。围绕SO导入Python有很好的答案,比如this one -
但是,如果您的目标是在运行(而不是在导入之后)脚本时执行特定功能,则可以使用
if __name__ == "__main__"条件
标签: python-3.x cmd