【问题标题】:(Ubuntu) Python3 script runs fine but under Apache/wsgi it can't find selenium module(Ubuntu) Python3 脚本运行良好,但在 Apache/wsgi 下找不到 selenium 模块
【发布时间】:2020-02-24 09:02:20
【问题描述】:

以下是我安装 selenium 时的结果:

ubuntu@ip-172-31-23-71:~$ pip3 install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
100% |████████████████████████████████| 911kB 1.6MB/s
Collecting urllib3 (from selenium)
  Downloading https://files.pythonhosted.org/packages/e8/74/6e4f91745020f967d09332bb2b8b9b10090957334692eb88ea4afe91b77f/urllib3-1.25.8-py2.py3-none-any.whl (125kB)
100% |████████████████████████████████| 133kB 9.2MB/s
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.13.1    // so the installation worked.
You are using pip version 8.1.1, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

这是我的python脚本(helloworld.py):

#!/usr/bin/python3

from selenium import webdriver

def testPrint():
    return "Test print"

def application(environ,start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'Hello world! \n' + testPrint() + \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]

正常情况下编译:

ubuntu@ip-xxx-xx-xx-xx:/var/www/html/pythonscrape$ python3 helloworld.py
ubuntu@ip-xxx-xx-xx-xx:/var/www/html/pythonscrape$

如你所见,编译“from selenium import webdriver”这一行没有问题

ubuntu@ip-xx-xx-xx-xx:/var/www/html/pythonscrape$ python3
Python 3.5.2 (default, Oct  8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>>

所以你看不到错误。

但是,当我通过浏览器/Apache/wsgi 运行它时,它找不到我的 selenium 模块。这是日志输出:

[Fri Feb 21 14:43:34.952035 2020] [wsgi:error] [pid 15279] [client 108.210.66.3:64700] mod_wsgi (pid=15279): Exception occurred processing WSGI script '/var/www/html/pythonscrape/helloworld.py'.
[Fri Feb 21 14:43:34.952130 2020] [wsgi:error] [pid 15279] [client 108.210.66.3:64700] Traceback (most recent call last):
[Fri Feb 21 14:43:34.952207 2020] [wsgi:error] [pid 15279] [client 108.210.66.3:64700]   File "/var/www/html/pythonscrape/helloworld.py", line 3, in <module>
[Fri Feb 21 14:43:34.952318 2020] [wsgi:error] [pid 15279] [client 108.210.66.3:64700]     from selenium import webdriver
[Fri Feb 21 14:43:34.952396 2020] [wsgi:error] [pid 15279] [client 108.210.66.3:64700] ImportError: No module named selenium

这是我的 helloworld.conf 文件:

WSGIScriptAlias /helloworld /var/www/html/pythonscrape/helloworld.py

ErrorLog /var/www/html/pythonscrape/error_log.txt

<Directory /var/www/html/pythonscrape/>
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
</Directory>

如何让 selenium 模块被 Apache/wsgi 识别?

【问题讨论】:

  • selenium 安装在您的项目所在的同一驱动器上吗?我回答了一个类似的问题here
  • 您好 Kosaro,我如何找出 selenium 的安装位置?当我输入“which selenium”时,它返回空白。
  • 在python终端import seleniumselenium.__file__应该给你模块的文件路径
  • 嗨 Kosaro,它显示 - /home/ubuntu/.local/lib/python3.5/site-packages/selenium/__init__.py 我需要在我的配置中包含它吗?跨度>

标签: python apache selenium


【解决方案1】:

您正在以“ubuntu”用户身份安装模块,但 Apache 在不同的安全用户帐户下运行。您的 Apache 服务器不会知道用户安装了 selenium 模块,也不会看到 selenium 模块。

快速解决方法是为所有用户全局安装该模块。更复杂的解决方案是创建 Apache 服务器使用的虚拟环境。

对于快速修复(全局安装):

sudo pip3 install selenium

Collecting selenium
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
    100% |################################| 911kB 1.8MB/s 
Collecting urllib3 (from selenium)
  Downloading https://files.pythonhosted.org/packages/e8/74/6e4f91745020f967d09332bb2b8b9b10090957334692eb88ea4afe91b77f/urllib3-1.25.8-py2.py3-none-any.whl (125kB)
    100% |################################| 133kB 7.1MB/s 
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.8
You are using pip version 8.1.1, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

该模块现在安装在全局位置 (/usr/local) 并且可供 Apache 使用:

Python 3.5.2 (default, Oct  8 2019, 13:06:37) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> selenium.__file__
'/usr/local/lib/python3.5/dist-packages/selenium/__init__.py'
>>> 

【讨论】:

  • 嗨 Chuck,快速修复“sudo pip3 install selenium”不起作用。我可以在apache中传递系统环境而不是创建虚拟环境吗?
  • @BrentHeigold - 如果您运行的是 ubuntu xenial,您的 Apache 服务器可能使用的是 python 2 而不是 python 3。脚本顶部的“#!/usr/bin/python3”仅适用来自 bash,而不是来自 mod_wsgi。请在您的测试中添加以下内容: import sys 然后在您的调试打印输出中还包括 ','.join(sys.path())。在我的电脑上,Apache 使用的是 python2,通过sudo pip install selenium 全局安装 selenium 的 python2 工作正常。
  • 要升级您的 Apache mod_wsgi 以使用 python3,以及来自 python3 的全局包,请使用:apt-get install libapache2-mod-wsgi-py3 请注意您的脚本不会在 wsgi py3 下运行,它需要字节输出。您需要将 OLD return [html] 更改为 NEW return [html.encode()]
  • 升级到py3模块后请务必重启Apache。要确认 mod_wsgi 使用的确切 Python 运行时,请查看:/var/local/apache2/error.log 在我的情况下,我之前看到 mod_wsgi: Runtime using Python/2.7.12. 然后在升级到 libapache2-mod-wsgi-py3 并重新启动后,它是 mod_wsgi: Runtime using Python/3.5.2.
  • 谢谢,它正在安装 libapache2-mod-wsgi-py3。我真的很感激这个 Chuck。
最近更新 更多