Mar-12-2020, 01:29 PM
Hello,
How to pass parameters while executing python script in IDLE environment ?
Thanks
How to pass parameters while executing python script in IDLE environment ?
Thanks
Execute script in IDLE with parameters
|
Mar-12-2020, 01:29 PM
Hello,
How to pass parameters while executing python script in IDLE environment ? Thanks
Mar-12-2020, 06:37 PM
I don't believe that idle (which is not a very good IDE, even though it comes with python) has a mechanism to pass parameters from within the GUI, but you can emulate this by setting sys.argv.
Example: >>> import sys >>> >>> sys.argv = [sys.argv[0], '-a', '-b', '-c', 'myfile.txt'] >>> >>> print(sys.argv) ['', '-a', '-b', '-c', 'myfile.txt'] >>>
Mar-13-2020, 08:10 AM
I'm afraid of being misunderstood.
Here is how the concerned script is executed from command line: python knn.py --dataset ../datasets/animals Can I execute knn.py from IDLE and pass it parameter dataset ?
You were understood perfectly fine and Larz gave you the only possible answer - IDLE is poor IDE and does not offer way to pass command line arguments when executing a script. Larz offered you a way to simulate it - by adding these lines you simulate as if there were command line arguments passed.
Of course it's better to use better IDE. I recommend VS Code, but there are plenty to choose from. We have nice tutorial on VSCode
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Well, probably I have to change for VSCode as you suggest.
But just to finish with IDLE ... is my comprehension of Larz approach correct if I proceed this way: import sys sys.argv = ['dataset', '../datasets/animals'] knn(sys.argv) As I just saw VSCode is Visual Studio extension. But I'm on Linux.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info. no, you should add import sys sys.argv = [sys.argv[0], '--dataset', '../datasets/animals']The first element of sys.argv is always the script itself the best would be to add the import statement at the top of the file and the other line as first line in if __name__ == '__main__': block. Note that this may vary little depending how your knn.py looks like.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
VSCode is not Visual Studio extension. It's a separate IDE from Visual Studio. There is indeed Python extension for Visual Studio, but that's not it.
https://code.visualstudio.com/ Look also at our tutorial. Many of us are on Linux or both Linux and Windows and use it on Linux. Here is the GitHub repo for the extension and this is the GitHub repo for VSCode
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Quote:This time I have added tags for youOk, sorry for that. Returning to the issue, is this sequence of commands (typing in IDLE) correct (without modifying knn.py) import sys sys.argv = [sys.argv[0], '--dataset', '../datasets/animals'] knn(sys.argv)Concerning IDLE alternative for Linux ... probably PyCharm ? Quote:VSCode is not Visual Studio extension. It's a separate IDE from Visual Studio. There is indeed Python extension for Visual Studio, but that's not it.Ok, I did not notice this message before posting the mine. Nevertheless which is better in your opinion (sorry for this probably stupid question) - VSCode or PyCharm ?
without
knn(sys.argv) you don't need it. And again - if you put it at the top it will work, but if your knn.py is properly structured, the second line should go in 'if __name__ == '__main__': block(Mar-13-2020, 08:47 AM)Pavel_47 Wrote: Concerning IDLE alternative for Linux ... probably PyCharmdid you read my second post? I used PyCharm in the past, but currently using VSCode on both Linux and Windows and I like it very much. PyCharm is too heavy compared to VSCode. For third time - look at the tutorial. Of course it's up to you. There are people that use PyCharm for one reason or another.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Mar-13-2020, 05:17 PM
Expanding just a little - I suggest you might install Anaconda3. That includes VSCode, another IDE that you might try called Spyder, as well as Jupyter Lab and Jupyter Notebook, among others. You can create environments and see what works best for you. Multi-platform, I have it on my Windows and Linux machines and it works pretty much the same.
|
|