Tuesday, November 29, 2022
HomeArtificial IntelligenceCommand line arguments to your Python script

Command line arguments to your Python script


Final Up to date on February 26, 2022

Engaged on a machine studying undertaking means we have to experiment. Having a technique to configure your script simply will provide help to transfer sooner. In Python, now we have a technique to adapt the code from command line. On this tutorial, we’re going to see how we are able to leverage the command line arguments to a Python script that will help you work higher in your machine studying undertaking.

After ending this tutorial, you’ll be taught

  • Why we wish to management a Python script in command line
  • How we are able to work in a command line effectively

Let’s get began.

Command line arguments to your Python script. Photograph by insung yoon. Some rights reserved

Overview

This tutorial is in three elements, they’re

  • Working a Python script in command line
  • Engaged on the command line
  • Different to command line arguments

Working a Python script in command line

There are numerous methods to run a Python script. Somebody could run it as a part of a Jupyter pocket book. Somebody could run it in an IDE. However in all platforms, it’s at all times attainable to run a Python script in command line. In Home windows, you may have the command immediate or PowerShell (and even higher, the Home windows Terminal). In macOS or Linux, you may have the Terminal or xterm. Working a Python script in command line is highly effective as a result of you’ll be able to go in extra parameter to the script.

The next script permits us to go in values from the command line into Python:

After we save these few strains right into a file, and run it in command line with an argument:

you will note it takes our argument, convert it into integer, add one into it and print. The record sys.argv comprises the identify of our script and all of the arguments (all strings), which within the above case, it’s ["commandline.py", "15"].

Once you run a command line with extra sophisticated set of arguments, it takes some effort to course of the record sys.argv. Subsequently Python supplied the library argparse to assist. That is to imagine GNU-style, which could be defined utilizing the next instance:

The elective arguments are launched by “-” or “--“, which a single hyphen is to hold a single character “quick choice” (resembling -a-B and -v above), and two hyphens are for a number of characters “lengthy choices” (resembling --exclude and --ignore-existing above). The elective arguments could have extra parameters, resembling in -B 1024 or --exclude="*.pyc", the 1024 and "*.pyc" are parameters to -B and --exclude respectively. Moreover, we may have obligatory arguments, which we simply put them into the command line. The half 192.168.0.3:/tmp/ and ./ above are examples. The order of obligatory arguments are necessary. For instance, the rsync command above will copy recordsdata from 192.168.0.3:/tmp/ to ./ as an alternative of the opposite approach spherical.

The next is to copy the above instance in Python utilizing argparse:

Should you run the above script, you will note:

That is to imply you didn’t present the obligatory arguments for src and dest. Maybe the perfect motive to make use of argparse is to get a assist display screen without cost for those who provded -h or --help because the argument, like the next:

Whereas the script did nothing actual, for those who supplied the arguments as required, you will note this:

The parser object created by ArgumentParser() has a parse_args() methodology that reads sys.argv and returns a namespace object. That is an object that carries attributes and we are able to learn utilizing args.ignore_existing for instance. However normally it’s simpler to deal with if it’s a Python dictionary. Therefore we are able to convert it into one utilizing vars(args).

Normally for all elective arguments, we offer the lengthy choice and generally additionally the quick choice. Then we are able to entry the worth supplied from the command line utilizing the lengthy choice as the important thing (with hyphen changed with underscore, or the single-character quick choice as the important thing if we don’t have a protracted model). The “positional arguments” should not elective and their names are supplied within the add_argument() operate.

There are a number of kinds of arguments. For the elective arguments, generally we use them as a boolean flags however generally we count on them to herald some knowledge. Within the above, we use motion="store_true" to make that choice set to False by default and toggle to True whether it is specified. For the opposite choice resembling -B above, by default it expects extra knowledge to go following it.

We will additional require an argument to be a selected sort. For instance the -B choice above, we are able to make it to count on integer knowledge by including sort like the next

and if we supplied the improper sort, argparse will assist terminate our program with an informative error message:

Engaged on the command line

Empowering your Python script with command line arguments can convey it to a brand new stage of reusability. First, let’s have a look at a easy instance on becoming an ARIMA mannequin to GDP time sequence. World Financial institution collected historic GDP knowledge of many nations. We will make use of the pandas_datareader bundle to learn the info. Should you haven’t put in it but, you need to use pip (or conda for those who put in Anacronda) to put in the bundle:

The code for the GDP knowledge that we use is NY.GDP.MKTP.CN, we are able to get the info of a rustic within the type of a pandas DataFrame by

after which we are able to tidy up the DataFrame a bit utilizing the instruments supplied by pandas:

then becoming an ARIMA mannequin and use the mannequin for prediction just isn’t troublesome. Within the following, we match utilizing the primary 40 knowledge factors and forecast for subsequent 3. Then examine the forecast with the precise when it comes to relative error:

Placing all of it collectively, and just a little sharpening, the next is the whole code:

This script prints the next output

The above code is brief however we made it versatile sufficient by holding some parameters in variables. We will change the above code to make use of argparse so we are able to change some parameters from the command line, as follows:

If we run the code above in a command line, we are able to see it will probably now settle for arguments:

Within the final command above, we go in -c NO to use the identical mannequin to the GDP knowledge of Norway (NO) as an alternative of Sweden (SE). Therefore, with out the danger of messing up the code, we reused our code to a distinct dataset.

The ability of introducing command line argument is that we are able to take a look at out our code with various parameters simply. For instance, we need to see if ARIMA(1,1,1) mannequin is an efficient mannequin for predicting GDP and we need to confirm with totally different time window of the nordic nations:

  • Denmark (DK)
  • Finland (FI)
  • Iceland (IS)
  • Norway (NO)
  • Sweden (SE)

We need to examine for the window of 40 years however with totally different beginning factors (since 1960, 1965, 1970, 1975). Is dependent upon the OS, you’ll be able to construct a for loop in Linux and mac utilizing the bash shell syntax:

or, because the shell syntax permits, we are able to put all the things in a single line:

and even higher, give some data at every iteration of the loop, and we get our script run a number of occasions:

Should you’re utilizing Home windows, you need to use the next syntax in command immediate:

or the next in PowerShell:

each ought to produce the identical end result.

Whereas we are able to put related loop inside our Python script, generally it’s simpler if we are able to do it on the command line. It may very well be extra handy once we are exploring totally different choices. Furthermore, by taking the loop exterior of the Python code, we could be assured that each time we run the script is impartial as a result of we is not going to share any variables between iterations.

Utilizing command line arguments just isn’t the one technique to go in knowledge to your Python script. At the very least, there are a number of different methods too:

  • utilizing atmosphere variables
  • utilizing config recordsdata

Setting variables are options out of your OS to maintain small quantity of information in reminiscence. We will learn atmosphere variables in Python utilizing the next syntax:

For instance, in Linux, the above two-line script will work with the shell as follows

and in Home windows, the syntax inside command immediate is comparable:

You may additionally add or edit atmosphere variables in Home windows utilizing the dialog in Management Panel:

So we could maintain the parameters to the script in some atmosphere variables and let the script adapt its habits like establishing command line arguments.

In case now we have a whole lot of choices to set, it’s higher to save lots of the choices to a file fairly than overwhelming the command line. Is dependent upon the format we selected, we are able to use the configparser or json module from Python for studying Home windows INI format or JSON format respectively. We may use the third occasion library PyYAML to learn YAML format.

For the above instance on operating ARIMA mannequin on GDP knowledge, we are able to modify the code to make use of YAML config file:

and the YAML config file is known as as config.yaml, which its content material is as follows:

Then we are able to run the above code and acquiring the identical end result as earlier than. The JSON counterpart could be very related, which we use the load() operate from json module:

and the JSON config file, config.json could be

Chances are you’ll be taught extra concerning the syntax of JSON and YAML to your undertaking. However the thought right here is that we are able to separate the info and algorithm for higher reusability of our code.

Additional studying

This part gives extra sources on the subject in case you are trying to go deeper.

Libraries

Articles

Books

Abstract

On this tutorial, you’ve see how we are able to make use of the command line for extra environment friendly management of our Python script. Particularly, you realized

  • How we are able to go in parameters to your Python script utilizing the argparse module
  • How we are able to effectively management the argparse-enabled Python script in a terminal below totally different OS
  • We will additionally use atmosphere variables, or config recordsdata to go in parameters to a Python script



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments