DEV Community

Cover image for Pass parameters and flags in Python and configure installers
Israel-Lopes
Israel-Lopes

Posted on • Updated on

Pass parameters and flags in Python and configure installers

Passing parameters

To pass parameters to a Python file, simply add the desired parameters after the file name on the command line. For example:

machine@machine~ python my_file.py parameter1 parameter2
Enter fullscreen mode Exit fullscreen mode

Within your code file, you can access parameters passed on the command line using the ys.argv function. This function returns a list with the passed parameters, where the first element of the list (index 0) is always the name of the Python file being executed.

For example, if you have a my_file.py file with the following code:

import sys

print(sys.argv)
Enter fullscreen mode Exit fullscreen mode

and run it in the terminal with the parameters parameter1 and parameter2:

machine@machine~ python my_file.py parameter1 parameter2

Enter fullscreen mode Exit fullscreen mode

The printed result will be:

['my_file.py', 'parameter1', 'parameter2']
Enter fullscreen mode Exit fullscreen mode

You can then access each of the parameters passed on the command line using the index of the list returned by sys.argv. For example, the first parameter would be sys.argv[1], the second would be sys.argv[2], and so on.

Inserting flags

To insert flags , you can use the following format:

machine@machine~ python file.py --flag1 value1 --flag2 value2
Enter fullscreen mode Exit fullscreen mode

Where flag1 and flag2 are the names of the flags you want to pass, and value1 and value2 are the values for those flags. You can pass as many flags as you want, just add more --flag value to the above command.

To read these flags in your code, you can use the argparse module:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--flag1', type=str, default='default_value1')
parser.add_argument('--flag2', type=int, default=0)

args = parser.parse_args()

print(args.flag1)
print(args.flag2)
Enter fullscreen mode Exit fullscreen mode

This will create an args object that contains the values of the flags passed in the command. You can use the args.flag1 and args.flag2 attributes to access the values of flags in your code.

Configuring installers for our scripts

There are several tools and libraries you can use to create an installer for your script, such as setuptools, distutils or cx_Freeze.

The simplest way to create an installer for your script is to use setuptools. First, you need to create a file called setup.py with the following content:

from setuptools import setup

setup(
    name='exif_reader',
    version='1.0',
    scripts=['exif_reader.py']
)
Enter fullscreen mode Exit fullscreen mode

Next, you need to create a file called MANIFEST.in with the following content:

include exif_reader.py
Enter fullscreen mode Exit fullscreen mode

This will include your exif_reader.py script in the installer. Finally, you can build the installer using the following command:

machine@machine~ python setup.py sdist
Enter fullscreen mode Exit fullscreen mode

This will create a file called exif_reader-1.0.tar.gz which is the installer for your script. You can send this file to others and they will be able to install your script using the following command:

machine@machine~ pip install exif_reader-1.0.tar.gz
Enter fullscreen mode Exit fullscreen mode

Texto alternativo da imagem)

Top comments (0)