Python Forum

Full Version: Error when running .py file but not when entering exact same code in CLI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
#!/usr/bin/python3

import requests
from bs4 import BeautifulSoup
url = "https://pythonprogramming.net/parsememcparseface/"
resp = requests.get(url)
txt = resp.text
soup = BeautifulSoup(txt, 'lxml')
The following error is now being produced on unchanged code that was previously working (2 days ago):
Traceback (most recent call last):
  File "./xtest.py", line 10, in <module>
    soup = BeautifulSoup(txt, 'lxml')
  File "/usr/lib/python3/dist-packages/bs4/__init__.py", line 165, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
Additionally, at the same point the error message is returned, a new sub-folder is created:
__pycache__
Containing 1 compiled/binary file:
lxml.cpython-36.pyc

If I run exactly the same code via the python3 CLI, no such error is returned, no such sub-folder is created and I can view and process the response text as expected.

I have removed and reinstalled python3-bs4, but the problem continues.

Any assistance would be much appreciated.
Is python3-lxml installed?
Yes indeed.

$ apt list | grep python3-lxml

python3-lxml/bionic-updates,bionic-security,now 4.2.1-1ubuntu0.1 amd64 [installed,automatic]
Just in case there was a problem with the python3-lxml package, I removed then reinstalled it, but the problem remains.