0

I have trying to call a python code from bar.php like

$cmd = "python foo.py";
system($cmd);

It runs with no problem. However, foo.py should save a file on disk. And it wasn't there. I somehow figured out that the user running the web server is www-data.

I tried running the php script from command line with user www-data $ php bar.php It runs OK and saved the file, so user www-data should have permission to write to the directory.

I noticed that there is a vote on "would not be helpful to other user". However, when you have a php service and you have code written in other language like python, knowing this is necessary.

Thanks for all suggestions.

I suspect it is b/c I use Matplotlib in the python code in foo.py, I have code like

pl.scatter(data,...)
pl.plot(data,...)
pl.legend()
pl.save('path/to/bam.png')

Maybe when calling from the web, it can draw images. Is that possbile? If so, is there any way to fix it? Thanks!

5
  • 1
    You'll have to debug the script. Maybe it's not writing the file where you think it is? Commented Dec 14, 2012 at 7:23
  • Well, I run in the command line, and it behaves exactly like what I want. On web browser, it shows the same output to stdout. Commented Dec 14, 2012 at 7:28
  • Are you sure that the python script gets run when called from PHP? If you are 100% that it does, then check permissions of the folder you are writing into. Commented Dec 14, 2012 at 7:29
  • Yes, I am. Because the python script prints something to stdout and it shows up on the web browser as well. I used www-data user, which is the user that runs PHP, in command line run the python script, it can save file on disk. It is very strange. Commented Dec 14, 2012 at 7:31
  • 1
    Try printing the current working directory, directory where Python will save the file, current user etc. from Python. This way you can see if the values are what you expect them to be. Commented Dec 14, 2012 at 7:38

1 Answer 1

2

I can only speculate due to the amount of information you are providing, but I think this is your problem. Your Python script is probably using relative paths, which means the files it creates will appear in your working directory, or relative to it.

To confirm this, and find out where your files are currently going, you could print the current working directory when in python:

import os
print os.getcwd()

And to fix it, use paths relative to the python script instead, or absolute paths. Example:

import os
basedir = os.path.abspath(os.path.dirname(__file__))

file_path = os.path.join(basedir, 'my-file-name.txt')
Sign up to request clarification or add additional context in comments.

1 Comment

@AlfredZhong: When you write pl.save('path/to/bam.png'), you are saving the file to a location relative to the working directory. The working directory is not necessarily the directory of the script. The working directory can be anywhere depending on how and who ran the script. You have to use absolute paths when you don't have control over where the script is run from. My answer explains how to achieve absolute paths extrapolated from the script location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.