简体   繁体   中英

Python import BeautifulSoup not working

I have a Python script that wants to use BeautifulSoup:

from bs4 import BeautifulSoup

When I run the script from the command line, it works fine. When I run the script externally, from a browser, it dies on that line. The web logs say:

ImportError: No module named bs4

I've also tried, with the same result:

import bs4
import BeautifulSoup

I installed the module from a tarball, and it now resides in a folder that is in my Python path:

/usr/home/myName/.local/lib/python2.7/site-packages/

I have made sure the permissions for the bs4 directory, and all.py and.pyc files in the folder allow execution ( chmod 775 *.py ), and I've checked to see that both internally and externally the same version of Python is being run ( 2.7.9 (default, Jan 12 2015, 16:33:18) [GCC 4.2.1 20070719 [FreeBSD]] )

You have not installed BeautifulSoup for the benefit of all the users on your computer. Instead, you installed only for the benefit of the user named "myName". In particular, you haven't installed it for the use of the user that runs the web server (often named 'www' or 'www-data').

If you can, install BeautifulSoup in the system-wide location.

Otherwise, you can modify your script like so:

import sys
sys.path[0:0] = ['/usr/home/myName/.local/lib/python2.7/site-packages/']
from bs4 import BeautifulSoup

Thanks to @robᵩ (phi) for pointing out what was wrong: because this script was on a shared server, I was unable to give execute permission to the "user" running the web server (in this case, "nobody"). I was able to run the script using ssh, since I was the user in that case.

Because I was unable to convince the hosting service to install BeautifulSoup for all users, I ended up using a different library, lxml.html , which my host server does install with Python.

(I found lxml.html a pleasure to work with, as well)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM