Python pyCharm命令行参数不起作用
我已经阅读了很多关于这方面的帖子,并尝试了所有方法,但不知何故,我无法在pyCharm中将命令行参数传递给我的python代码。我已经做了以下工作 A) 请看第一个图片附件,当我运行的代码,我得到这个错误Python pyCharm命令行参数不起作用,python,pycharm,Python,Pycharm,我已经阅读了很多关于这方面的帖子,并尝试了所有方法,但不知何故,我无法在pyCharm中将命令行参数传递给我的python代码。我已经做了以下工作 A) 请看第一个图片附件,当我运行的代码,我得到这个错误 C:\Automation\myTest\venv\Scripts\python.exe -s C:/Automation/myTest/myTest.py ABC XYZ ============================================================
C:\Automation\myTest\venv\Scripts\python.exe -s C:/Automation/myTest/myTest.py ABC XYZ
======================================================================
ERROR: ABC (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'ABC'
======================================================================
ERROR: XYZ (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'XYZ'
我尝试了与所附图片相同的方法,没有-s选项
C:\Automation\myTest\venv\Scripts\python.exe C:/Automation/myTest/myTest.py ABC XYZ
======================================================================
ERROR: ABC (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'ABC'
======================================================================
ERROR: XYZ (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'XYZ'
代码
在不知道
myTest.py
中的内容的情况下,这有点像猜测,但是如果在运行文件时调用unittest
或测试运行程序,那么这些参数将被解释为要运行的测试模块。换句话说,unittest
正在寻找名为ABC
和XYZ
的Python测试模块。如果ABC.py
和XYZ.py
不存在,您将得到与您看到的错误完全相同的错误类型
如果您想拥有自己的参数,除了unittest
的期望之外,还可以通过直接传入参数来修改对main()
的调用。例如,如果希望自己使用第一个to参数(在程序名之后),并将其余参数传递给unittest
:
if __name__ == '__main__':
arg1, arg2 = sys.argv[1:3]
unittest.main(argv=sys.argv[3:])
这将第一个参数分配给您可以使用的变量,然后将任何其他参数传递给unittest
。所以你可以在最初的问题中打电话:
python myTest.py ABC XYZ
或者您可以这样做,并运行特定的测试:
python myTest.py ABC XYZ path.to.test.module
您有名为
ABC
和XYZ
的测试模块?您可能需要向myTest.py
提供源代码。source myTest.py非常大,ABC和XYZ是参数如果name=='main':unittest.main()获取错误-->unittest.main(argv=sys.argv=sys.argv[3:])^SyntaxError:无效的语法对不起,这是一个拼写错误。该行应该是unittest.main(argv=sys.argv[3:])
python myTest.py ABC XYZ path.to.test.module