-2

I have a module which contains the python code and I execute it using the following command:

python script.py \
  --eps 12 \
  --minpts \
  --train \
  --predict \
  --lower_case \
  --input_file data.csv \
  --dev_file devdata.csv \
  --output_dir /output/

All I want to do is to execute the above command through a python function. Is there any way of doing it?

wjandrea
  • 16,334
  • 5
  • 30
  • 53
Vas
  • 598
  • 1
  • 3
  • 12
  • 1
    I'm not quite clear on what you're looking for. Are you asking how to execute that CLI command from within Python? – glibdud Sep 18 '19 at 14:25
  • Hi @gliddud Thank you for responding. And yes I would like to execute that CLI within python code – Vas Sep 18 '19 at 14:38
  • 1
    Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – glibdud Sep 18 '19 at 14:42
  • It would be simpler and more efficient to make your script a proper module, import it, and call relevant functions. May I kindly suggest you read [this](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) and [this](https://docs.python.org/3/tutorial/modules.html) ? – bruno desthuilliers Sep 19 '19 at 10:20

2 Answers2

-1

I don't know why everyone is having such difficulty with this question, it's perfectly clear, unfortunately it's also difficult to do what you want because python uses implicit data types, and that's uncommon. As a result all command line arguments are passed to python as strings.

I'd check this out for the details:

https://www.tutorialspoint.com/python/python_command_line_arguments.htm

but the tldr is to have inside your python script:

import sys

eps = int(sys.argv[sys.argv.index('eps')+1])
minpts = True if '-minputs' in sys.argv else False
…

Obviously this isn't ideal, or pretty but it is quick and easy. Alternatively you can use the argparser library:

https://docs.python.org/3/library/argparse.html

For a more robust and user friendly solution. Hope this helps

A.

Edit: I was missing the ' around eps

Andrew Louw
  • 476
  • 3
  • 6
  • `eps` is not defined in line 3 – wjandrea Sep 18 '19 at 17:13
  • 1
    What do you mean by "implicit data types"? Duck typing? What does that have to do with command line arguments? – wjandrea Sep 18 '19 at 17:14
  • Quite literally any command line arguments into any program in any language are passed as strings. The strings are parsed and then stored appropriately by converting them to the correct type/opening the specified file/etc. How else do you expect the command line to interpret other language-specific objects? – SyntaxVoid supports Monica Sep 18 '19 at 17:31
  • I've fixed the mistake, it should work now. By implicit data types I am referring to the automatic type conversion python does, e.g when you say a = 1 it automatically assumes a is an integer. This is relevant because users that only use python might not know that this is uncommon and so would possibly expect the console to pass arguments "- eps 12" to python as [str: eps, int: 12]. – Andrew Louw Sep 19 '19 at 10:04
-3

Command-Line Arguments

import sys

print 'version is', sys.version

The first line imports a library called sys, which is short for "system". It defines values such as sys.version, which describes which version of Python we are running. This command tells the python interpreter installed in your machine to run program sys-version.py from the current directory.

Here's another script that does something more interesting:import sys print 'sys.argv is', sys.argv

  • 1
    How does this answer the question? – wjandrea Sep 18 '19 at 18:57
  • 1
    This seems to be lifted from [here](https://scw-ss.github.io/2018-03-15-lund-python-novice-inflammation/10-cmdline/#command-line-arguments) or something similar. Plagiarism is not against the rules here, but it's definitely not cool. Please put any quoted material in a blockquote, and cite your sources. – wjandrea Sep 18 '19 at 18:58