我正在通过
从PHP运行Shell脚本shell_exec("path/to/script path/to/argument");
通过终端执行时相同的脚本运行完美,但在通过PHP执行时会抛出以下错误:
Traceback (most recent call last):
File "/home/file1.py", line 9, in <module>
import spacy
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/__init__.py", line 5, in <module>
from .deprecated import resolve_model_name
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/deprecated.py", line 8, in <module>
from .cli import download
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/__init__.py", line 1, in <module>
from .download import download
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/download.py", line 9, in <module>
from .link import link_package
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/link.py", line 4, in <module>
import pip
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 16, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/lib/python3/dist-packages/pip/vcs/subversion.py", line 9, in <module>
from pip.index import Link
File "/usr/lib/python3/dist-packages/pip/index.py", line 30, in <module>
from pip.wheel import Wheel, wheel_ext
File "/usr/lib/python3/dist-packages/pip/wheel.py", line 39, in <module>
from pip._vendor.distlib.scripts import ScriptMaker
File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/scripts.py", line 14, in <module>
File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/compat.py", line 66, in <module>
ImportError: cannot import name 'HTTPSHandler'
在上面的代码中,/ home / file1.py在shell脚本中执行。
所以基本上我认为python在通过PHP执行时找不到spacy但是在通过终端执行时找到了 请建议一种我可以解决问题的方法。
更新
所以我尝试查找Spacy的安装位置,在official website上,我发现可以通过在终端中运行以下命令找到路径。
python -c "import os; import spacy; print(os.path.dirname(spacy.__file__))"
所以我做了这个,在终端上我得到了这个输出:
/home/myusername/.local/lib/python3.5/site-packages/spacy
但是当我使用
通过PHP执行相同的操作时echo shell_exec('python3.5 -c "import os; import spacy; print(os.path.dirname(spacy.__file__))" 2>&1');
我得到了这个输出:
Traceback (most recent call last):
File "", line 1, in
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/__init__.py", line 5, in
from .deprecated import resolve_model_name
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/deprecated.py", line 8, in
from .cli import download
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/__init__.py", line 1, in
from .download import download
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/download.py", line 9, in
from .link import link_package
File "/home/myusername/.local/lib/python3.5/site-packages/spacy/cli/link.py", line 4, in
import pip
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 16, in
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/lib/python3/dist-packages/pip/vcs/subversion.py", line 9, in
from pip.index import Link
File "/usr/lib/python3/dist-packages/pip/index.py", line 30, in
from pip.wheel import Wheel, wheel_ext
File "/usr/lib/python3/dist-packages/pip/wheel.py", line 39, in
from pip._vendor.distlib.scripts import ScriptMaker
File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/scripts.py", line 14, in
File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/compat.py", line 66, in
ImportError: cannot import name 'HTTPSHandler'