0

I am trying to a run a Python file via command line or within the interpreter using:

import sys
import subprocess
subprocess.call([sys.executable, "file.py", "arg1", "arg2", "arg3"])

However the program returns a TypeError: "cannot concatenante 'str' and 'numpy.float64' objects".

What I can not understand, is when I run the program line by line in the interpreter, there are no such errors and everything is fine.

I have no idea as to where to start debugging this but I suspect it could have something to do with my setup.

I have a 64bit version of Python and 32bit version of Python installed in Windows 7. Both versions of Python are 2.7. (This is due to usage of some modules which are only available in 32 bits - as such the program above is being running in the 32 bit version). The Environment Path variable has been edited to only use the 32bit version.

I'm not sure what other information is relevant but please let me know and I'll dig it up.

Basically I just want to be able to run the program from command:

python program.py arg1 arg2 arg3

Any help is greatly appreciated

1
  • Why don't you just run python file.py arg1 arg2 arg3? Commented Apr 5, 2013 at 1:39

1 Answer 1

3

You are most likely passing the arguments to your function without converting them from their string representations - when you test the code in the interpreter and provide it with numbers everything will, of course, work as expected. If you are doing something like this:

# Wild guess at what your code actually looks like

if __name__ == "__main__":
    # main_function(23.1, 44.9, 12.21)  # This works when uncommented ... why?
    main_function(sys.argv[1], sys.argv[2], sys.argv[3])  # This breaks ... why?

then know that when you call:

python program.py 23.1 44.9 12.21

you are really calling main_function with the following:

main_function("23.1", "44.9", "12.21")

You'll need to explicitly convert your arguments to floats with the float type constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes of course! Gosh that was actually a rather silly question. Many thanks for your help.

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.