Search code examples
pythonxmlbeautifulsoupelementtree

AttributeError: module 'copy' has no attribute 'deepcopy'


I'm Actually New to Python and BS4. And I Decided to create a script that will scrape a website, oscarmini.com to be precise, the code was running fine untill today when I wanted to modify it, I keep getting errors, in the little knowledge I have about Exceptions and Error, there's nothing wrong with the code it seems to be from the importation of 'bs4' module..

from bs4 import BeautifulSoup as BS
import requests
url = 'https://oscarmini.com/2018/05/techfest-2018.html'
page = requests.get(url)
soup = BS(page.text, 'lxml')
mydivs = soup.find("div", {"class": "entry-content"})
soup.find('div', id="dpsp-content-top").decompose()
print(mydivs.get_text())
input()

Below is the error message I get.

Traceback (most recent call last):
    File "C:/Users/USERNaME/Desktop/My Programs/Random/Oscarmini- 
Scrapper.py", line 1, in <module>
    from bs4 import BeautifulSoup as BS
File "C:\Users\USERNaME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\__init__.py", line 35, in <module>
    import xml.etree.cElementTree as default_etree
File ":\Users\USERNaME\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\cElementTree.py", line 3, in <module>
    from xml.etree.ElementTree import *
File "C:\Users\USERNaME\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1654, in <module>
    from _elementtree import *
AttributeError: module 'copy' has no attribute 'deepcopy'

Process finished with exit code 1

Please I really need help on this..


Solution

  • I encountered the same problem. And I finally found that the problem is I have another script named copy.py and it shadows the original copy module.

    You can show the real path for the copy module with print(copy.__file__) just before the exception occurs and see whether it is intended.

    You can also list your PATHONPATH environment variable with: print(os.environ['PYTHONPATH'].split(os.pathsep)) just before the line that causes the exception, and see whether there are something unexpected.