我需要在PHP下启动Python脚本。要启动我使用的python脚本:
$mystring = system('python testpy.py ');
testpy.py
与php文件位于同一目录中。如果python脚本的内容是一个简单的" Hello world"有用。但是,如果我需要启动一个使用某些库的python脚本,它就无法工作。例如:
#!/usr/local/lib/python2.7
# encoding: utf-8
import sys;
sys.path.append('/usr/local/lib/python2.7/site-packages')
import os, site;
help(site)
import matplotlib.pyplot as plt
print "Hello, World!"
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
但在我的浏览器中,我看到了:
模块站点上的帮助:NAME站点 - 将第三方软件包的模块搜索路径附加到sys.path。 FILE /usr/lib/python2.7/site.py MODULE DOCS http://docs.python.org/library/site说明***************************** *********************************** *初始化期间会自动导入此模块。 * ************************************************* ***************在早期版本的Python(最多1.5a3)中,需要使用特定于站点的模块的脚本或模块会放置
import site'' somewhere near the top of their code. Because of the automatic import, this is no longer necessary (but code that does it still works). This will append site-specific paths to the module search path. On Unix (including Mac OSX), it starts with sys.prefix and sys.exec_prefix (if different) and appends lib/python/site-packages as well as lib/site-python. On other platforms (such as Windows), it tries each of the prefixes directly, as well as with lib/site-packages appended. The resulting directories, if they exist, are appended to sys.path, and also inspected for path configuration files. For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used. A path configuration file is a file whose name has the form .pth; its contents are additional directories (one per line) to be added to sys.path. Non-existing directories (or non-directories) are never added to sys.path; no directory is added to sys.path more than once. Blank lines and lines beginning with '#' are skipped. Lines starting with 'import' are executed. For example, suppose sys.prefix and sys.exec_prefix are set to /usr/local and there is a directory /usr/local/lib/python2.5/site-packages with three subdirectories, foo, bar and spam, and two path configuration files, foo.pth and bar.pth. Assume foo.pth contains the following: # foo package configuration foo bar bletch and bar.pth contains: # bar package configuration bar Then the following directories are added to sys.path, in this order: /usr/local/lib/python2.5/site-packages/bar /usr/local/lib/python2.5/site-packages/foo Note that bletch is omitted because it doesn't exist; bar precedes foo because bar.pth comes alphabetically before foo.pth; and spam is omitted because it is not mentioned in either path configuration file. After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary additional site-specific customizations. If this import fails with an ImportError exception, it is silently ignored. FUNCTIONS abs__file__() Set all module' __file__ attribute to an absolute path addpackage(sitedir, name, known_paths) Process a .pth file within the site-packages directory: For each line in the file, either combine it with sitedir to a path and add that to known_paths, or execute it if it starts with 'import '. addsitedir(sitedir, known_paths=None) Add 'sitedir' argument to sys.path if missing and handle .pth files in 'sitedir' addsitepackages(known_paths) Add site-packages (and possibly site-python) to sys.path addusersitepackages(known_paths) Add a per user site-package to sys.path Each user has its own python directory with site-packages in the home directory. aliasmbcs() On Windows, some default encodings are not provided by Python, while they are always available as "mbcs" in each locale. Make them usable by aliasing to "mbcs" in such a case. check_enableusersite() Check if user site directory is safe for inclusion The function tests for the command line flag (including environment var), process uid/gid equal to effective uid/gid. None: Disabled for security reasons False: Disabled by user (command line option) True: Safe and enabled execsitecustomize() Run custom site specific code, if available. execusercustomize() Run custom user specific code, if available. getsitepackages() Returns a list containing all global site-packages directories (and possibly site-python). For each directory present in the global
PREFIXES {{1} USER_BASE, this function will find its `site-packages` subdirectory depending on the system environment, and will return a list of full paths. getuserbase() Returns the `user base` directory path. The `user base` directory can be used to store data. If the global variable
USER_SITE``尚未初始化,此函数也将设置它。 main()makepath(* paths)removeduppaths()从sys.path中删除重复的条目,同时使它们成为绝对setBEGINLIBPATH()OS / 2 EMX端口具有可选的扩展模块,它们作为DLL执行双重任务(并且必须使用.DLL文件扩展名)用于其他扩展。需要修改库搜索路径,以便在模块导入期间找到它们。使用BEGINLIBPATH,以便它们位于库搜索路径的开头。 setcopyright()设置' copyright'和'信用' in builtin setencoding()设置Unicode实现使用的字符串编码。默认为' ascii',但如果您愿意尝试,可以更改此设置。 sethelper()setquit()定义新的内置函数'退出'和'退出'这些是在调用时使解释器退出的对象。每个对象的repr包含一个如何工作的提示。 DATA ENABLE_USER_SITE = True PREFIXES = [' / usr',' / usr'] USER_BASE =' /var/www/.local' USER_SITE =' /var/www/.local/lib/python2.7/site-packages'
我在Apache的Ubuntu网络服务器上工作,在mysite conf文件中我插入了关于cgi脚本的选项:
is not initialized yet, this function will also set it. getusersitepackages() Returns the user-specific site-packages directory path. If the global variable
但没有结果。