[英]running python function with arguments from cmd.exe
I have Python function that takes 1 arguments 我有带1个参数的Python函数
def build_ibs(Nthreads,LibCfg): # Nthreads is int,LibCfg as string
import os # module os must be imported
import subprocess
import sys
I use following in cmd.exe(on Win7) to call it 我在cmd.exe(在Win7上)中使用以下命令来调用它
C:>cd C:\SVN\Python Code
C:\SVN\Python Code>C:\Python27\python.exe build_libs(4,'Release')
that throws error 引发错误
using following
使用以下
C:>cd C:\SVN\Python Code
C:\SVN\Python Code>C:\Python27\python.exe 4 'Release' # dosn't work
C:\SVN\Python Code>C:\Python27\python.exe 4 Release # dosn't work
does nothing, and no error is displayed even. 什么也不做,甚至不会显示任何错误。
What is the correct way to call it both in cmd.exe or even Python shell command line? 在cmd.exe甚至Python Shell命令行中调用它的正确方法是什么?
Thanks 谢谢
sedy 塞迪
You can't just call a function from the command line - it must be inside a file. 您不能只从命令行调用函数-它必须在文件内部。 When you type
python filename.py
at the command line, what it does is feed the contents of filename.py
into the Python interpreter with the namespace set to __main__
. 在命令行中输入
python filename.py
时,其作用是将filename.py
的内容输入到名称空间设置为__main__
的Python解释器中。
So when you type Python.exe 4 'Release'
it tries to find a file named 4
. 因此,当您键入
Python.exe 4 'Release'
它将尝试查找名为4
的文件。 Since this file does not exist, Windows returns an Errno 2 - File not found. 由于此文件不存在,Windows将返回Errno 2-找不到文件。
Instead, put your code into a file - lets say test.py : 相反,将您的代码放入文件中-假设test.py :
test.py : test.py :
def build_libs(Nthreads,LibCfg): # Nthreads is int,LibCfg as string
import os # module os must be imported
import subprocess
import sys
# ...
if __name__=='__main__':
numthreads = sys.argv[1] # first argument to script - 4
libconfig = sys.argv[2] # second argument
# call build_libs however you planned
build_libs(numthreads, libconfig)
Then run from the command line: C:\\Python27\\python.exe test.py 4 Release
In the directory that test.py is saved in. 然后从命令行运行:
C:\\Python27\\python.exe test.py 4 Release
在保存test.py的目录中。
Update: If you need to use build_libs
in multiple files, it's best to define it in a module, and then import it. 更新:如果需要在多个文件中使用
build_libs
,最好在模块中定义它,然后将其导入。 For example: 例如:
mod_libs/__init__.py - empty file mod_libs / __ init__.py-空文件
mod_libs/core.py : mod_libs / core.py :
def build_libs(...):
....
# function definition goes here
test.py : test.py :
import sys
import mod_libs
if __name__ == '__main__':
mod_libs.build_libs(sys.argv[1], sys.argv[2])
sys.argv
or argparse
). sys.argv
或argparse
)。 For example: 例如:
C:\SVN\Python Code> py
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import build_libs
>>> build_libs.build_libs(4, 'Release')
Or, in your build_libs.py
, import the sys
module at the top of the script, and then run the function based on its arguments at the end of the script: 或者,在
build_libs.py
,在脚本顶部导入sys
模块,然后在脚本末尾基于其参数运行该函数:
import sys
...
print(build_libs(int(sys.argv[1]), sys.argv[2]))
Then at your OS's command line: 然后在您的操作系统的命令行中:
C:\SVN\Python Code> py build_libs 4 Release
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.