Python - Script

> Procedural Languages > Python > Python - Engine

1 - About

Language - Script in Python are source file that can be run.

Advertising

3 - Run

Running a script:

Example:

  • declaration of:
    • two console scripts called foo and bar,
    • and a GUI script called baz
setup.py
setup(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ],
        'gui_scripts': [
            'baz = my_package_gui:start_func',
        ]
    }
)

4 - Python

With python

If you are in a virtual environment, see virtualenv - run a script

# -m run library module as a script (terminates option list)
python -m module
  • source read from script file)
python file
python -
python -c cmd 
 
#Example:
python -c "print('Hello')"
Advertising

5 - Declaration

5.1 - console_scripts

In Python - setup.py

setuptools generates scripts for you:

  • with the correct extension (on Windows an .exe file, no need to create an file association)

Distributions can specify console_scripts entry points, each referring to a function which can be used as a command

When a console_scripts aware installer such as pip installs the distribution, it will create a command-line wrapper for each entry point.

automatic-script-creation

6 - Location

6.1 - Path

Path of the script itself

import os 
script_path = os.path.realpath(__file__)
script_dir_path = os.path.dirname(script_path)

6.2 - Installation

Installation of package as Scripts

Example:

  • Global: C:\Users\gerard\AppData\Roaming\Python\Python37\Scripts
  • User: UserPath\Python37\Scripts
Advertising

7 - Structure

7.1 - Linux

Begin your scripts with your interpreter. See Linux - Script (Shebang)

#! /usr/local/bin/python

or

#! /usr/bin/python

You can find them by executing the whereis commando:

$ whereis python
python: /usr/bin/python /usr/bin/python2.4 /usr/lib/python2.4 /usr/include/python2.4 /usr/share/man/man1/python.1.gz
lang/python/engine/script.txt · Last modified: 2019/01/09 15:38 by gerardnico