简体   繁体   中英

running python function with arguments from cmd.exe

I have Python function that takes 1 arguments

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

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?
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__ .

So when you type Python.exe 4 'Release' it tries to find a file named 4 . Since this file does not exist, Windows returns an Errno 2 - File not found.

Instead, put your code into a file - lets say 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.

Update: If you need to use build_libs in multiple files, it's best to define it in a module, and then import it. For example:

mod_libs/__init__.py - empty file

mod_libs/core.py :

def build_libs(...):
    ....
    # function definition goes here

test.py :

import sys
import mod_libs
if __name__ == '__main__':
    mod_libs.build_libs(sys.argv[1], sys.argv[2])
  1. If you want to use Python code as in your first attempt, you need to start the Python interpreter.
  2. If you want to call a Python function from your OS's command line as in your second attempt, you need to use the proper modules (like sys.argv or 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM