[英](Ubuntu) Python3 script runs fine but under Apache/wsgi it can't find selenium module
以下是我安裝 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>
我該怎么做才能讓 Apache/wsgi 識別 selenium 模塊?
您以“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'
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.