Shoot python scripts with the Ubuntu console

1

Good morning,

I am working in Python on Ubuntu , I need to create an application that executes various actions through common console commands without calling the python3 entity.

for example:

user@usuario-de-ubuntu:~$ ejecutaComando --ParametroDeComando

This would trigger the call to some process written in pyhton.

Any suggestions. Thank you in advance.

    
asked by Brayan Zavala 07.12.2017 в 20:58
source

1 answer

2

To execute a script without having to explicitly call the interpreter, or pass the path of the script itself and without using the extension of the file, you have to follow four steps in principle:

  • Delete the extension : in Linux, unlike Windows, the extension does not matter at all when defining a file as executable or not. Generally the executables do not have extension for convenience, so if we want to execute our script without having to put the .py ,. py3 , .pyw , etc we simply delete it from the file name.

    If we want to keep the extension or even change the name we will use in the terminal to call it, we can use a symbolic link (among many other options):

    $ ln -s ./foo.py ./foo
    
  • Add correct shebang line to link to the appropriate interpreter: with shebang you know the set of two characters #! start of files that are interpretable executables ( bash, python, etc). This pair of characters is followed by the absolute path of the interpreter that has to execute that file.

    For example, the line:

      #!/usr/local/bin/python
    

    tells the OS to open the file with the Python interpreter located in /usr/local/bin . We can also link to the interpreter of a virtual environment if we want, we just have to find the appropriate interpreter route.

    However, the previous form is not recommended because between systems the location of the interpreter can vary, so we use env that is responsible for providing the appropriate route automatically according to the $ PATH:

    • Script compatible with Python 2 and 3:

      #!/usr/bin/env python
      
    • Script for Python 2:

      #!/usr/bin/env python2
      
    • Script for Python 3:

      #!/usr/bin/env python3
      
  • Make the executable file : When we call a script with $ python foo.py we are actually calling the executable python and passing the script to execute. As the interpreter has execution permissions we have no problems. If we want to be able to execute the script by its name or with double click it is necessary to give it execution permissions. The handling of permits is a world in itself, by way of example:

    • Execution permissions for the user who owns the file:

      $ chmod u+x ruta/a/mi/script.py
      
    • Execution permissions for the owner and group user:

      $ chmod ug+x ruta/a/mi/script.py
      
    • Execution permissions for all system users:

      $ chmod a+x ruta/a/mi/script.py
      

    Always remember that the permissions must be restrictive, if we foresee that the script will only be used by x users, only give permission to those users.

  • With the previous steps we can execute the script simply by indicating the route. If we are located in the script directory, we can execute them with:

    $ ./foo
    

    For security reasons it is not allowed to use only the name of the file, it is necessary to reference the file that contains it ( ./ ). We can also execute it indicating the absolute or relative path from other directories, eg: $ /home/usurio/scripts/foo .

  • Add the script to the environment variables: If we want to execute the script from any working directory without specifying its path ( $ foo arg1 arg2 ), as for example it is done with django-admin we need to add the script to the path. This already depends on what we want to do with it and we have an immense variety of possibilities, for example:

    • We can put it in directories that are already in the $ PATH as /bin/ or /usr/bin/ .

    • Create a symbolic link to the script in one of these folders, eg: $ ln -s ./foo /usr/bin/foo

    • Add the script directory to the $ PATH only for the current session. When the session closes it will cease to have effect:

      $ export PATH=${PATH}:/ruta/absoluta/a/mi/directorio
      
    • Add the directory permanently to $ PATH: for this we can edit the file ~/.profile (or ~/.bashrc ) by adding the line export PATH=$PATH:/ruta/a/mi/directorio/ , so that it will take effect immediately in the current session make $ source ~/.bashrc .

    This is also a separate world and an extensive subject as well as the issue of permissions, the options depend on what we want to do, the distro or shell used can also influence, but the idea is this.

  • With the four steps correctly completed we should be able to execute our script in the terminal just by doing:

    $ foo argumento1 argumento2...
    
        
    answered by 08.12.2017 / 14:53
    source