Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在终端/命令提示符下运行方法_Python - Fatal编程技术网

如何使用python在终端/命令提示符下运行方法

如何使用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

我在这里有文件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.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