如何使用python在终端/命令提示符下运行方法
我在这里有文件1:如何使用python在终端/命令提示符下运行方法,python,Python,我在这里有文件1: #File1 class MyClass1(): def abc(self) --- def efg(self) --- 下面是文件2: #File2 from File1 import MyClass1 def test1() callfromfile1 = Myclass1() callfromfile1.abc() def test2() callfromfile1 = Myclass1() callfromfile1.ef
#File1
class MyClass1():
def abc(self)
---
def efg(self)
---
下面是文件2:
#File2
from File1 import MyClass1
def test1()
callfromfile1 = Myclass1()
callfromfile1.abc()
def test2()
callfromfile1 = Myclass1()
callfromfile1.efg()
if __name__== "__main__":
test()
问题:
如何仅在终端/命令提示符下调用test2
方法
注:1.我正在使用python3
2.我是否需要在上面的
file2
中添加另一个“类(例如MyClass2)”,以便专门调用test2
3.请给出一些示例以供参考。如果
file2
实际上被称为file2.py
,并且在您的PYTHONPATH
中,您可以说
python3 -c 'import file2; test2()'
如果没有,也许可以试试
(cat file2; echo 'test2()') | python3
第三种可能的解决办法是使最后一条更加复杂
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
test()
elif 'test2' in sys.argv[1:]:
test2()
# maybe more cases here in the future
就这样说吧
python3 file2 test2
获取
elif
分支。除了从selenium import webdriver导入之外,是否有与selenium
相关的内容?我遗漏了什么吗?对不起,我不知道python和selenium的具体语法是什么,因为我正在使用它进行selenium测试。thnxIt是一个纯粹的Pythonic
概念,与Selenium
无关。从selenium import webdriver和selenium标签中删除行,更新问题,以清除混淆完成!thnx表示感谢。您愿意向脚本传递其他命令行参数吗?顺便说一句,名为的库使得将Python源文件中的单个函数公开给调用shell变得非常容易。是的,它是名为file1.py
和file2.py