Run Function from the Command Line

Run function from the command line

With the -c (command) argument (assuming your file is named foo.py):

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

Alternatively, if you don't care about namespace pollution:

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

And the middle ground:

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

How can I run a function from a script in command line?

If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the source or . command and then simply call the function. See help source for more information.

How to run python function from command line

You can use the flag -c while doing python like this:

python -c 'isPrime(11)'

you can also import this way as well

python -c 'import foo; foo.isPrime(2)'

or do this

python -c 'from foo import isPrime; foo.isPrime(n)'

How do I call a specific python function in a file from the command line?

You could either call python with the command option:

python -c "from file_name import function;function()"

or if you want this function to be called every time you execute the script you could add the line

if __name__ == "__main__":
function()

This will then only execute this function if the file is executed directly (i.e. you can still import it into another script without "function" being called).

Run function in script from command line (Node JS)

No comment on why you want to do this, or what might be a more standard practice: here is a solution to your question.... Keep in mind that the type of quotes required by your command line may vary.

In your db.js, export the init function. There are many ways, but for example:

module.exports.init = function () {
console.log('hi');
};

Then call it like this, assuming your db.js is in the same directory as your command prompt:

node -e 'require("./db").init()'

To other readers, the OP's init function could have been called anything, it is not important, it is just the specific name used in the question.

Run function in script from command line (Node JS) using ES6 Modules

You can use command line arguments. For getting command line arguments you can use process.argv. Maybe you can change your program something like that:

function name(params) {
console.log("Hello World");
}
if (process.argv[2] === "name") {
name("");
}

Now when you write "node schedules.js name" to your terminal your function will be called.

How to run a python function from Windows 10 command prompt in 'python filename.py funcname' format?

I found a solution that works for me:

After digging deeper, I realized that windows passes a sys.argv input that contains a string of all the command prompt inputs used to call the file.

In my case of 'python hello.py world', the system would pass ['hello.py','world'] as the argument for sys.argv. By creating a dictionary of callable functions, and then matching the string of the sys.argv with its respective function in the dictionary, I am able to execute the code as desired.

New code:

print('Hello')

def world():
print('Hello World')

import sys
callable_functions = {'world':world}
callable_functions[sys.argv[1]]()

Command prompt input and output now:

In: python hello.py world
Out: 'Hello'
Out: 'Hello World'

Is it possible to call a python function in a terminal?

This can get complicated. A basic solution is to get the function object from the global namespace.

def hi():
print("Hello")

def bye():
print("Sorry, I'm staying")

while True:
try:
cmd = input("what up? ")
globals()[cmd.strip()]()
except KeyError:
print("Invalid")

You could use your own dictionary instead of the global namespace, at the cost of the need to initialize it. That can aid in implementing something like a help system. You could even put all of your commands in their own module, use its namespace instead of your own dict.

Then there is the question of supplying parameters. If you want to do something like that, you could split the string on commas, or something like that.

You could even explore "Domain Specific Languages in Python" for more complicated but expressive solutions.



Related Topics



Leave a reply



Submit