#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2

    Calling some function() from the python script via PHP?


    Hi guys,

    I've been trying to call a function I needed from my Python Script. Is there any way to do that, I'm now wondering.

    For example my python script is like this:

    name.py

    Code:
    def Hey():
       print "Hey!"
    
    def Yo():
      print "Yo!"
    so as u can see when I execute the script it will show nothing in the PHP because the function does not call to use it. So I want to call it from the PHP page.

    Is there a way to do this?
    I did try :

    PHP Code:
    <php?
    echo 
    exec("python name.py Yo()");
    ?> 
    but it doesn't work at all,

    Thanks forwarding
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,072
    Rep Power
    9398

    Moved to Python


    If you can find a way to do it using the command line then you can do it with exec().

    By the way, I don't see any way of doing that. Maybe the Python folks know better.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Try
    Code:
    <php? 
    echo exec("python -c 'import name;name.Yo()'"); 
    ?>
    semi-colon is a statement separator which seems to work if the statements can all be written at the same indentation level.

    Comments on this post

    • requinix agrees : knew of -c, hadn't considered using import
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2
    Thank you !! It works, could you please tell me one more question Supposed that I adapt a code to accept the sys.argv[1] like this where should I put the value in? I tried but failed.

    Code:
    import sys
    
    Input = sys.argv[1]
    def Hey():
       print "Hello!Python!Coming!"
    
    def Yo():
       print "Yo!!"+Input
    Code:
    "python -c 'import HelloPython <Input??>;HelloPython.Yo()'";
    because normally we can do something like:

    Code:
    "python HelloPython.py <Input>";
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481

    Had to look up this information


    $ python -c 'import sys;print(sys.argv)' hi world
    ['-c', 'hi', 'world']


    The arguments follow the command. Thus,
    Code:
    echo exec("python -c 'import name;name.Yo()' argue with me");
    http://docs.python.org/2/using/cmdline.html#command-line
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2
    Wow, I get it now, thank you very much. This is very useful!!

IMN logo majestic logo threadwatch logo seochat tools logo