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 keep it in the 21st century, and Python, in its queue, has excellent syntax and a host of other advantages, therefore, 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:


    • xonsh is only about Python 3, but this is the norm .
    • xonsh hasn't been released yet (version 0.8.3 at the time of writing), apparently according to the developers, not all of the desired features are implemented, but according to my feelings everything works (if you deal with the differences mentioned below).

    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) not requiring superuser rights, although I only recently switched to such a scheme, I used to install into system folders, register it in /etc/shellsand change the shell with the command chsh, but at first glance everything also works with the new method and it seems to me more correct, I don’t want to clutter 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 quote 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 have .profilepaths to the local folder with binaries somewhere in the PATH $HOME/.local/bin, but they are added only if they exist, so you need to restart the terminal so that this code runs and the xonsh binary can be run and viewed.
    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 before you install 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 installed and it remains to make xonsh a 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 reads it too) 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 in fact in the python console and can perform calculations such as directly on the command line, for example to find out how much it will 2+2.


    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 in the config imported the module, oswhich 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 resulted file is more for demonstration of possibilities, and here the last three lines allow conveniently using virtual environments, an example of which use 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 add-on, avoxwe just need to go to the project folder to activate the virtual environment, we do source ./bin/activatenot need to do any strange things:


    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 him 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 yet grown together , and has not reached 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 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 are the differences 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 '{}' ';'

    There are some differences from bash 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 to use (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.


    Also popular now: