Pycharm and sys.argv arguments
In PyCharm the parameters are added in the Script Parameters
as you did but, they are enclosed in double quotes ""
and without specifying the Interpreter flags like -s
. Those flags are specified in the Interpreter options
box.
Script Parameters box contents:
"file1.txt" "file2.txt"
Interpeter flags:
-s
Or, visually:
Then, with a simple test file to evaluate:
if __name__ == "__main__":
import sys
print(sys.argv)
We get the parameters we provided (with sys.argv[0]
holding the script name of course):
['/Path/to/current/folder/test.py', 'file1.txt', 'file2.txt']
Pass command line arguments to pycharm from file
PyCharm lets you have unlimited named runtime configurations, as you appear to know, so I am a little puzzled that you ask. Click on the current configuraton name to the left of the green Run arrow, top right, then Edit Configurations.
These configurations live in workspace.xml
. Nothing stopping you from checking it in.
For programs that take complex command line parameters it is traditional to provide a way to read the values from a named file, typically introduced by @
. In argparse
you specify this as follows:
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
Arguments read from a file must by default be one per line.
When using PyCharm to debug script that accepts command-line arguments, can I specify to accept one or more parameters?
Not sure if this is exactly what you meant, but if you specify that Parameter field with quotation marks around some arguments, they will be treated as one, e.g.:
# Parameter field in PyCharm: -c "1 2" -f filename.txt
>>> sys.argv[1:]
['-c', '1 2', '-f', 'filename.txt'] # 1 and 2 together in the same string
Note, however, that ArgumentParser
will expect all the arguments separately, even if you pass nargs='+'
or nargs='*'
to add_argument()
. So, if you are using the ArgumentParser
class, you probably do not want to use quotation marks:
# Parameter field in PyCharm: -c 1 2 -f filename
>>> sys.argv[1:]
['-c', '1', '2', '-f', 'filename.txt']
More or less, what the ArgumentParser
class will do with nargs='+'
or nargs='*'
is:
- Start scanning the arguments one by one from left to right
- As soon as it finds a keyword it recognizes (e.g.
-c
), it will consume all the arguments that follow until it reaches another keyword that it recognizes (e.g.-f
) or the end of the list - All those intermediate arguments that it found will be put into a list and associated to the first argument (e.g.
-c
)
Even if you are not using the ArgumentParser
class and you have your own code for the parsing, I would suggest adopting a similar parsing strategy. It is probably more intuitive for users of the script, plus you would not need to split the arguments manually at some point later (e.g. '1 2'.split()
).
Related Topics
Convert Rgb Color to English Color Name, Like 'Green' With Python
How to Kill a Python Child Process Created With Subprocess.Check_Output() When the Parent Dies
Trouble Installing Scipy in Virtualenv on a Amazon Ec2 Linux Micro Instance
Tkinter: Attributeerror: Nonetype Object Has No Attribute ≪Attribute Name≫
What Is the Maximum Recursion Depth in Python, and How to Increase It
Local Variables in Nested Functions
How to Sleep Selenium Webdriver in Python For Milliseconds
Deploying a Minimal Flask App in Docker - Server Connection Issues
Why Is Nothing Drawn in Pygame At All
How to Terminate a Python Subprocess Launched With Shell=True
Cqlsh Connection Error: 'Ref() Does Not Take Keyword Arguments'
System-Wide Mutex in Python on Linux
How to Sort a Dictionary by Value
Iterating Over Dictionaries Using 'For' Loops
How to Get Keyboard Input in Pygame