3

hey yall. Im running python on a webserver from dreamhost. I am using their install of python and am using a lastfm module that can be found here: http://code.google.com/p/python-lastfm/

to get it to import properly i do this

import sys
sys.path.append("/home/myusername/build/Python-2.5/Lib/site-packages/")
import lastfm

since the lastfm module is installed there.

When I use putty to ssh into my server, i can simply run python test.py and it works perfectly. But when i run it from a php script with

exec("python test.py");

it suppossedly does not work and the script doesnt run. it runs perfectly fine when i do

import lastfm

and then have other things after, but when i actually try to do something with the module like:

import lastfm
api=lastfm.Api(api_key)

it does not run. once again i can run the script using the same python install in a shell and it executes fine. So something must be happening that goes wrong when i run it from the php script. I figured it would be running the exact same python and everything. I checked other posts and they say it may be something with file permissions, but ive put every file to 777 and it still doesnt work. idk what the problem could be. thanks in advance everyone.

deceze
  • 510,633
  • 85
  • 743
  • 889
stackVidec
  • 291
  • 1
  • 5
  • 8

4 Answers4

1

Try using the full path to the python executable. For example:

exec("/usr/bin/python test.py")

You can find the full path from the command line using the which command:

$ which python /usr/bin/python

gregjor
  • 21,802
  • 1
  • 23
  • 16
  • i did which python and got /usr/bin/python and did what you said and it unfortunately did not work. edit: i just tested it and it does run the version of python from usr/bin and it works when i remove the lastfm module, so it does execute, but still has the same problem as before. – stackVidec Feb 27 '11 at 05:51
1

Whatever error python is raising would be going to the child's stderr. Try either telling php to read from stderr, or (in python) do this:

import sys
sys.stderr = sys.stdout
tangentstorm
  • 7,183
  • 2
  • 29
  • 38
0

For Windows users:

$output = null;
exec('C:\\Python27\\python.exe C:\\sud.py', $output);
echo var_export($output, TRUE);

The code i was searching whole day ^^
That's why - hope it'll help somebody.

karlisup
  • 672
  • 12
  • 22
-1

For Windows User - Thanks to Karlisup my PHP file could read python. I'm using BITNAMI WAMP in EC2 Amazon, my python file (leadatos.py) and php file are on htdocs folder.

My calling was

<?php
  passthru('C:\\Python27\\python.exe leadatos.py');
 ?>

The last line of my Python file was print "message".

Hope it words!