2

I am trying to execute a python script from php like this:

$output= shell_exec("/usr/bin/python grabData.py);

The only problem is, I'm using a locally installed module. So I get the error :

Traceback (most recent call last):
File "/the path/grabData.py", 
line 2, in <module>
import xlrd
ImportError: No module named xlrd

Is there any way for me to make it so that Apache can use my modules? Note: like i said in the title I don't have root access.

2 Answers2

0

virtualenv may give you the setup needed to work with your own Python environment in a shared hosting world.

https://virtualenv.pypa.io/en/latest/

Ryan
  • 1
0

Make sure that your module is in the Python search path, which may differ from yours for the Apache user. You can either set the path in the environment variable or from the Python code itself before trying to import it.

Note the mention of an installation-dependent default in the [following][1]:

6.1.2. The Module Search Path

When a module named spam is imported, the interpreter searches for a file named spam.py in the directory containing the input script and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python.

Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation-dependent default. This allows Python programs that know what they’re doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported. This will generally be an error. See section Standard Modules for more information.

See these questions:

Expand Python Search Path to Other Source

How to import a module given the full path?

Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Yes I think the problem is that the Apache user has a different PATH, so I need to force it look in my installation? That's what I don't know how to do. Just as a note the script works when called from my user account. – Johnathan Smith May 11 '15 at 22:29