4

I found an answer on another Stack Overflow question about how to call a specific function def from within a Python file on the command line, however the function called doesn't take any arguments:

$ python -c 'from foo import hello; print hello()'

(I stripped the print statement as it seemed superfluous for my needs and am just calling the function in that case.)

Several answers say to use argument parsing, but that would require changes to several files that already exist and is undesirable.

The last answer on that question presents how to do what I want in Bash (I need to know how to do it in PowerShell).

$ ip='"hi"' ; fun_name='call_from_terminal'
$ python -c "import ${fun_name} as cft; cft.test_term_fun(${ip})"
hi

Here is my Python code:

def operator (string):
    print("Operator here, I got your message: ", string)

And from PowerShell I want to call it doing something like:

$ python -c 'from myfile import operator; operator("my message here")'

The literal command I'm typing into PowerShell:

python -c 'from testscript import operator; operator("test")'

The literal error message I'm getting back:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'test' is not defined
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tuffwer
  • 1,027
  • 9
  • 23
  • What have you tried? PowerShell has no problem running executables with command-line arguments. – Bill_Stewart Jan 13 '16 at 18:14
  • @Bill_Stewart All powershell is doing here is calling the python interpreter. I apologize, I did a bad job of stating that I don't want to pass the argument as an additional argument (like one normally would on the CLI) to the python file that would then have to do some extra work to grab the argument and call the function. I want to call from the command line exactly as I would in python, with the arguments being passed to the function directly instead of as arguments to python. I mostly mention powershell for any syntax issues related to variables. – Tuffwer Jan 13 '16 at 18:19
  • "with the arguments being passed to the function directly instead of as arguments to python" - can you explain what you mean by this? Anything you specify on the python command line is "an argument to python". – Bill_Stewart Jan 13 '16 at 18:21
  • instead of: python -c "from foo import bar; bar() argument_to_bar" passing the argument to the bar function as a separate entry in the arg array I want to call it as: python -c "from foo import bar; bar(argument_to_bar)" passing the argument into the function at the same time I'm calling the function as I would inside the python file. Essentially I have a function in a python file that takes 1 string argument. I want to call just that function (not the entire python script file) and I want to pass it the argument entirely from cli without having to add any arg parsing to the original file. – Tuffwer Jan 13 '16 at 18:25
  • Again: What have you tried, and with what results? (and what suggests to you that this won't somehow work in PowerShell?) – Bill_Stewart Jan 13 '16 at 18:40
  • I've tried to type out exactly what I've written into powershell... it's the last code block in the question, passing the argument both as a string and as a variable containing a string. Python fails both ways complaining about what is being passed as 'not defined'. Why I think it won't just work out of the box, well besides the fact that it's not the usual way to pass parameters on a cli it hasn't worked when I tried it. What suggests to you that it should just work? – Tuffwer Jan 13 '16 at 18:44
  • Please update your question with the literal command you are typing at the PowerShell prompt, along with the literal error message you are getting. (Remember, we can't see your screen.) – Bill_Stewart Jan 13 '16 at 18:46
  • I've added what's literally showing on my screen, as I said before It's pretty much exactly what I've already told you. After some more digging (before I saw you're last comment) I've discovered that the " marks need to have a \ in front of them which is weird given that powershell uses ` as an escape character, but it seems to be working now. However I also added an execute line to the python script after the function definition. That line still executes as well instead of just the specific function being invoked. – Tuffwer Jan 13 '16 at 18:58
  • The error message is from python, not PowerShell. PowerShell is just passing along the string to the python command line. – Bill_Stewart Jan 13 '16 at 20:00
  • `python --% -c 'from testscript import operator; operator("test")'` – user4003407 Jan 13 '16 at 20:13
  • @PetSerAl When I run the command with with --% all I get is: File "", line one \n 'from \n SyntaxError: EOL while scanning string literal (I placed the \n characters so you would know where linebreaks in the output are. – Tuffwer Jan 13 '16 at 20:39
  • @Bill_Stewart Yes I know the error message is from python. The question has both python and powershell tags because python is being called from powershell. I stated in my very first comment to this question that all powershell is doing is calling the python interpreter. – Tuffwer Jan 13 '16 at 20:41
  • @Tuffwer Are you using PowerShell v2? `--%` should work in PowerShell v3+. – user4003407 Jan 14 '16 at 07:33
  • @PetSerAl $PSVersionTable says I've got Powershell 4. – Tuffwer Jan 14 '16 at 16:59

1 Answers1

7

I think I understand the problem. PowerShell passes double-quotes to the executable even when you specify single quotes (it's trying to be helpful). Using showargs.exe (see http://windowsitpro.com/powershell/running-executables-powershell):

PS C:\> showargs python -c 'from testscript import operator; operator("test")'
python -c "from testscript import operator; operator("test")"

You should be able to escape the " characters in your string to pass to the Python interpreter, either this way:

PS C:\> showargs python -c "from testscript import operator; operator(\""test\"")"
python -c "from testscript import operator; operator(\"test\")"

Or like this:

PS C:\> showargs python -c "from testscript import operator; operator(\`"test\`")"
python -c "from testscript import operator; operator(\"test\")"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62