📜 ⬆️ ⬇️

xonsh - python as shell replacement

Surprisingly, on Habré there is still no post about such a very interesting replacement of the shell as xonsh ( github ), from my point of view, the syntax of all shell'ov is terrible and I see no reason to save it in the 21st century, and Python, in its queue, has excellent syntax and a host of other advantages, so, in my opinion, it should be the default automation language, which is what xonsh is trying to achieve .


I use xonsh for a while, so I think I can tell you enough about it in order to start using it.


Reservations:



The main feature of xonsh is that it "magically" guesses what you entered - a python or shell command, and it works quite well.


You can insert a python code into shell commands using a dog .


I will not dwell on what the possibilities in xonsh are, this is understandable and clearly described in the documentation and all sorts of articles , from my point of view it is enough that you can get the normal syntax of the cycles in the shell:


worldmind@x ~ $ for i in range(3): ............... echo $SHELL 

Therefore, I will try to focus on what is not described or described poorly.


Installation


I will describe the installation (for Debian / Ubuntu) that does not require root privileges, although I only recently switched to such a scheme, I used to install into system folders, register it in /etc/shells and change the shell with the chsh command, but at first glance everything works also with the new method and it seems to me more correct, I don’t want to litter the system with packages not from repositories, but then everyone decides for himself.


Set pip if not yet:


 sudo apt-get install python3-pip 

We put xonsh (without sudo), I give a command that installs all optional dependencies to get all the buns conceived by the authors, if someone wants a minimal installation, you can remove the square brackets with the contents:


 pip3 install --user xonsh[ptk,pygments,proctitle,linux] 

Most likely, you already, somewhere in the .profile in the PATH, add the paths to the local folder with $HOME/.local/bin binaries, but they are added only if they exist, so you need to restart the terminal so that this code can work and the xonsh binary can was run and see.
Update as standard:


 pip3 install --user xonsh --upgrade 

venv


We put venv if we want to use the corresponding functionality (see further about vox):


 sudo apt-get install python3-venv 

All sorts of venv are sharpened for specific shells, so xonsh offers its own wrapper called vox , but for comfortable use, you should install the avox extension:


 pip3 install --user xontrib-avox 

Install pyenv


If there is a need for virtual environments with an arbitrary version of python, then you need to clone pyenv after setting the dependencies for python assembly :


 git clone https://github.com/pyenv/pyenv.git ~/.pyenv 

Further, in the config example, you can see the setting of a pair of environment variables for using pyenv.


Launch


Now we have everything set up and it remains to make a xonsh shell, in order not to change anything outside the user folder, I use the following code (based on SO ) for the bash (if you have another shell, then you know what to do, but do not use .profile since xonsh also reads it) added to .bashrc :


 # set default shell without editing /etc/shells if [ "${XONSH_VERSION:-unset}" = "unset" ] ; then export SHELL=$HOME/.local/bin/xonsh exec $HOME/.local/bin/xonsh -l fi 

We restart the shell and, if everything went well, you are already in xonsh as a matter of fact in the python console, for example, you can perform calculations directly on the command line, for example, find out how much 2+2 will be.


Customization


Before you begin to use, you should create a configuration file .xonshrc :


 aliases['g'] = 'git' import os local_bin = '{}/.local/bin'.format($HOME) if os.path.isdir(local_bin): $PATH.append(local_bin) $PYENV_ROOT = '%s/.pyenv' % $HOME $PATH.insert(0, '%s/bin' % $PYENV_ROOT) xontrib load vox $PROJECT_DIRS = ["~/projects"] xontrib load avox 

Restarting the shell to apply the new settings.


It is worth paying attention to the human data model - aliases are a dictionary, paths are a list, it seems obvious, but for some reason it is not always so.
Also, we imported the os module in the config, which means that it will already be available in our shell, so you can import the necessary modules and get your own, convenient environment.


The beginning of the above file is more to demonstrate the possibilities, but the last three lines make it convenient to use virtual environments, an example of which is used further.


Use of virtual environments


Create a project folder (avox expects all projects to be in $PROJECT_DIRS ):


 mkdir -p projects/test 

Create a virtual environment for this project:


 vox new test 

Thanks to the customized avox add- avox it’s enough to go to the project folder to activate the virtual environment, no strange source ./bin/activate to be executed:


 worldmind@x ~ $ cd projects/test/ (test) worldmind@x ~/projects/test $ pip install see ... (test) worldmind@x ~/projects/test $ python -c 'import see' 

Upon exiting the folder, the virtual environment is deactivated:


 (test) worldmind@x ~/projects/test $ cd worldmind@x ~ $ python3 -c 'import see' err>out | fgrep 'NotFound' ModuleNotFoundError: No module named 'see' 

At the same time, you can see more human work with the redirection of input-output streams , who have never forgotten how to do this in all sorts of bashers, let them be the first to throw a comment at me.


For the sake of completeness, I would like that in these virtual environments one could use an arbitrary version of python, for example, installed via pyenv, but has not grown together yet , and didn’t reach the hands itself.
UPD: Not so long ago, xonsh taught us how to use an arbitrary version of python in virtual environments.
Install the desired version of python (list of available pyenv install --list ):


 pyenv install 3.7.2 

Create a virtual environment with it:


 mkdir projects/projectwith3.7 vox new -p $PYENV_ROOT/versions/3.7.2/bin/python projectwith3.7 

Checking:


 (projectwith3.7) worldmind@x ~/projects/projectwith3.7 $ python --version Python 3.7.2 

Rake


The only thing I stumbled upon is the difference in escaping :


 find . -name data.txt -exec echo {} \; 

will not work, because escaping with a backslash does not work in xonsh and braces have special meaning, you need to use quotes, like this:


 find . -name .xonshrc -exec echo '{}' ';' 

Some differences from bash are in the form of a table in the documentation .


Conclusion


It seems to me that xonsh is a good contender for a normal shell of the future for everyone, and especially it should appeal to pythonists. Start using (installation without sudo makes it easier to roll back, you can simply delete the folder) to see if everything is there for you personally, maybe this is what you were looking for, but you were afraid to install.


')

Source: https://habr.com/ru/post/429892/


All Articles