1

I know this question has been asked in various variations but none of them seem to work for my specific case:

(btw all mentioned files are in my PATH)

I have a simple python script called test.py:

import sys

print('Hello world!')
print(sys.argv)

counter = 0

while True:
    counter +=1

The counter is just there to keep the command window open so don't worry about that.

When I enter

test.py test test 

into cmd I get the following output:

Hello world!
['C:\\Users\\path\\to\\test.py']

For some reason unknown to me the two other commands (sys.argv[1] and sys.argv[2]) are missing.

However when I create a .bat file like this:

@C:\Users\path\to\python.exe C:\Users\path\to\test.py %*

and call it in cmd

test.bat test test

I get my desired output:

Hello world!
['C:\\Users\\path\\to\\test.py', 'test', 'test']

I've read that the

%*

in the .bat file means that all command line arguments are passed to the python script but why are exactly these arguments not passed to the python script when I explicitly call it in cmd?

From what I've read all command line arguments entered after the script name should be passed to said script but for some reason it doesn't work that way.

What am I overlooking here?

1 Answer 1

0

I'm guessing that you need to actually run the script through the command line with C:\Users\path\to\python.exe test.py test test.

I'm not sure how Windows handles just test.py test test but from my limited experience it is probably just trying to open all 3 of those files. Since the first one (test.py) has a .py extension, it is opened with the Python Interpreter and is run automatically. The other two are not actually being passed in as an argument.

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

5 Comments

C:\Users\path\to\python.exe test.py test test unfortunately did not work. C:\Users\path\to\python.exe C:\Users\path\to\test.py test test however worked perfectly. How come the way I originally did it does not pass "test test" as arguments?
The original command doesn't work because you're not running test.py with the two arguments, you're telling windows to open three files called test.py, test and test. Only the first one exists, so windows opens it. I'm guessing the default behaviour when opening a .py file is to run it (in this case without arguments)
I reckon that makes sense. However it's a shame that one has to type the entire path even though the script is in the PATH variable.
You don't need to have the script in the path (and in fact it's not recommended). Standard practice is to put the python executable in the path, and then cd into the correct directory containing the script. So, cd folder\containing\test.py\file and after that you can just run python test.ty test test. If you keep CMD open then you only need to cd into the correct directory once at the start.
I just tried that and it worked perfectly. Thank you very much.

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.