4 ms·
Can you talk about how is it different than writing python scripts? Or writing test code in the python console?
by scottydelta 2y ago
Can you talk about how is it different than writing python scripts? Or writing test code in the python console?
- Neywiny 2y agoFeels like the only benefit is not having to write os.system(). Though maybe including shell scripts (like build environment setup stuff from other software) for more complicated commands? I may give this a try for a bit to find out
- shlomo_z 2y agoos.system doesn't actually change your environment: >>> os.getcwd() 'C:\\Users\\Admin\\Downloads' >>> os.system("cd Video") 0 >>> os.getcwd() 'C:\\Users\\Admin\\Downloads' >>>
- timonoko 2y agoThis the perfection, why would you want anything more? import os def sh(x): return os.popen(x).read().split("\n")
- brumar 2y agoFor me it shines when you need to write shell scripts but you prefer do some parts in python. As another commenter wrote os.system or calling the subprocess module works too, but I find it less satisfying.
- BeetleB 2y agoIn a xonsh script, you can have regular statements like "cd", "ls", and pretty much everything. It's just like Bash scripting, except in Python. You can mix and match Python statements with regular shell commands.
- echoangle 2y agoI have never tried it but I would probably prefer to use sh ( https://pypi.org/project/sh/ https://pypi.org/project/sh/ ) and have users install that before having to use a totally separate shell. Python is normally already there.
- BeetleB 2y agoCursory look at the docs: sh.ls("-l", "/tmp", color="never") Oh dear - no. A lot less ergonomic than "ls -l /tmp". If all you want is a convenient way to call commands from Python scripts, sh makes sense. The benefit of xonsh, though, is that it is a shell :-) So, for example, if you want to have a fancy prompt, you can use Python functions to construct your fancy prompt. If you want to manipulate your $PATH as a list (or any similar environment variable), you just treat it like a Python list and do whatever you want with it. I often write simple conditionals/for loops right on the prompt (not via a shell script). I use the Python syntax, but call whatever command I want and manipulate it. And it's really handy that the shell prompt is a Python prompt - you can import any Python library and play with it. I never run "python" to get to a Python prompt. I have it available at all times. I want to look at a docstring for a standard Python function? I just do it at the command line.