0

I have a script saved as workspace.py

import argparse
import os

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('title', type=str, help="Will be displayed as the title")
    parser.add_argument('-f', '--folder', help='Point to the folder you want to read from (defaults to current folder in command prompt)', type=str, default=os.getcwd())
    args = parser.parse_args()
    print(args)
    someFunction(args.folder, args.title)

Which I call from terminal with:

workspace.py myTitle

Resulting in the error

workspace.py: error: the following arguments are required: title

I have no idea why this is happening because I supply "myTitle" in the terminal. If I specify a default= for the title argument it works perfectly with that value. The part that is throwing me is it doesn't even get to the print(args) so I cannot see what the program thinks is what, but instead fails at args = parser.parse_args()

I tried to even redo the exact example at: https://docs.python.org/2/howto/argparse.html#introducing-positional-arguments (copied below)

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here")
args = parser.parse_args()
print args.echo

Running

workspace.py hello

Results in (after adding parenthesis to the print for 3.X)

workspace.py: error: the following arguments are required: echo

Is there something I'm missing? Why does it not just print "hello"? Is there some Python 3 specific syntax I'm missing or something?

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • How are you executing the script? With both `python workspace.py myTitle` and `./workspace.py myTitle`, it works on my machine (™). – edd Mar 06 '19 at 01:05
  • 1
    You could add, at the start, `import sys` and `print(sys.argv)`. This shows us what the parser has to work with. Also, what OS? WIndows by any chance? – hpaulj Mar 06 '19 at 01:08
  • @hpaulj I just did that and `workspace.py hello` results in `['C:\\Users\\me\\folder\\subfolder\\workspace.py']` Shouldn't there be a second item in this list of "hello"? I'm on Windows 10 – Reedinationer Mar 06 '19 at 01:12
  • Windows command window requires (or at least required) some special handling to pass arguments on through when using `batch` and `shebang` notation. But my memory of that dates way back to 3.2 :( https://docs.python.org/3/using/windows.html#arguments-in-shebang-lines – hpaulj Mar 06 '19 at 02:25
  • What does `ftype | findstr -i python` output in your (*cmd*) terminal? – CristiFati Mar 11 '19 at 15:43
  • @CristiFati `'-i' is not recognized as an internal or external command, operable program or batch file.` – Reedinationer Mar 11 '19 at 17:29
  • No way! Just the string as I posted, copy / pasted in the console, no dblquotes, nothing else? Or maybe written by hand? Anyway it's hard to believe. What about `ftype | findstr "ython"`? Are there some special settings on your terminal? – CristiFati Mar 11 '19 at 17:37
  • @CristiFati Hmm I guess I had a type because a straight copy/paste got me `C:\Users\rparkhurst>ftype | findstr -i python`; `Python.ArchiveFile="C:\Windows\py.exe" "%L" %*`; `Python.CompiledFile="C:\Windows\py.exe" "%L" %*`; `Python.File="C:\Windows\py.exe" "%L" %*`; `Python.NoConArchiveFile="C:\Windows\pyw.exe" "%L" %*`; `Python.NoConFile="C:\Windows\pyw.exe" "%L" %*` – Reedinationer Mar 11 '19 at 17:42
  • Those are *"%L"*s? I have *"%1"*s instead but on my machine both versions work fine. So are you sure you typed the script name correctly in your *cmd* (like in the error you got when you typed the command that I first sent)? Also `print args.echo` would raise a *SyntaxError*. What happens if you execute in the console `ftype Python.File="C:\WINDOWS\py.exe" "%1" %*` and then run your script? – CristiFati Mar 11 '19 at 18:10
  • @CristiFati `Access is denied.`; `Error occurred while processing: Python.File.`. I've posted an answer that should work for me. Apparently including `python` every time the function is called (while tedious and seemingly avoidable) allows the program to run correctly – Reedinationer Mar 11 '19 at 18:52
  • Yes, because your user is not an administrator. You could also take a look at https://stackoverflow.com/questions/52008516/how-do-i-set-default-app-for-a-file-extension-to-an-exe-on-windows-10-after/52009412#52009412. – CristiFati Mar 13 '19 at 17:17

1 Answers1

1

I've gotten it to work if I run python workspace.py someString instead of workspace.py someString. I do not understand why this version works, since command prompt obviously recognizes it as Python and runs it correctly until args = parser.parse_args(). There were no errors like 'workspace.py' is not recognized as an internal or external command, operable program or batch file. There was no problem importing modules either. Consider the below command prompt session if you are running into a similar error. Maybe you will simply have to include python in your commands like I have to...

C:\Users\rparkhurst\PycharmProjects\Workspace>workspace.py MyTitle
usage: workspace.py [-h] [-f FOLDER] title
workspace.py: error: the following arguments are required: title

C:\Users\rparkhurst\PycharmProjects\Workspace>python workspace.py MyTitle
Namespace(folder='C:\\Users\\rparkhurst\\PycharmProjects\\Workspace', title='MyTitle')
Reedinationer
  • 5,661
  • 1
  • 12
  • 33