0

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
enter image description here 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

1

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Aaron, it works and I also got the idea.However, if there were many files that needed to use build_libs then, calling it from within other files would be better than rewriting function definition again and again? in such case, assuming build_libs is put into a package and imported, would function call remain same as you have shown above?
Yeah, you can put it into a Python module. I edited the answer to show how you might do that.
0
  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

1 Comment

Tigerhawk,I tried this but did not work. I have updated question too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.