Python - Script

> Procedural Languages > Python > Python - Engine

1 - About

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

Advertising

3 - Structure

3.1 - Main / Entry point

3.2 - Shebang

On 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

4 - Properties

4.1 - Name

sys.argv[0] is the script name

sys.argv[0] 

See Python - Command Line Argument

Advertising

4.2 - Path

Path of the script itself

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

4.3 - Argument

5 - Type

5.1 - gui_scripts

In Python - setup.py

Example: declaration of a GUI script called baz

setup.py
setup(
    # other arguments here...
    entry_points={
        'gui_scripts': [
            'baz = my_package_gui:start_func',
        ]
    }
)

5.2 - console_scripts

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 in Python - setup.py, 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.

Example: declaration of two console scripts called foo and bar,

setup.py
setup(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ]
    }
)

More … automatic-script-creation

Advertising

5.3 - startup

6 - Management

6.1 - Run

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')"

6.2 - Search Path

Installation of package as Scripts

Example:

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