Table of Contents

About

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

Structure

Main / Entry point

Python - Main (Entry Point)

Shebang

On Linux, begin your scripts with your interpreter. See How to write a 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

Properties

Name

sys.argv[0] is the script name

sys.argv[0] 

See Python - Command Line Argument

Path

Path of the script itself

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

Argument

Python - Command Line Argument

Type

gui_scripts

In Python - setup.py

Example: declaration of a GUI script called baz

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

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(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ]
    }
)

More … automatic-script-creation

startup

Python - Startup script for the console ($PYTHONSTARTUP)

Management

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

Search Path

Installation of package as Scripts

Example:

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