ImportError: cannot import name chardet

谁说我不能喝 提交于 2019-12-20 03:17:02

问题


Hi i have written a python scrapper in which i am importing requests and Beautiful soup module . I am using python2.7 . I am crating Windows executable (.exe) from my Python script using py2exe module. For that i have created setup.py as following :

from distutils.core import setup
import py2exe
import requests

packages = [
    'requests',
    'requests.packages',
    'requests.packages.chardet',
    'requests.packages.urllib3',
    'requests.packages.urllib3.packages',
    'requests.packages.urllib3.contrib',
    'requests.packages.urllib3.util',
    'requests.packages.urllib3.packages.ssl_match_hostname',
]

setup(
    console=['CompanyScrapper-1.6.py']
    )

I have created .exe file by running command "python setup.py py2exe". But while running the .exe file on window it throwing following error:

Traceback (most recent call last):
  File "CompanyScrapper-1.6.py", line 13, in <module>
  File "requests\__init__.pyc", line 58, in <module>
  File "requests\utils.pyc", line 26, in <module>
  File "requests\compat.pyc", line 7, in <module>
ImportError: cannot import name chardet

i could not undaerstand why this chardet import error though i have included package into setup.py

Thanks you for looking into my issue.


回答1:


As you can see from the traceback -

Traceback (most recent call last):
  File "CompanyScrapper-1.6.py", line 13, in <module>
  File "requests\__init__.pyc", line 58, in <module>
  File "requests\utils.pyc", line 26, in <module>
  File "requests\compat.pyc", line 7, in <module>
ImportError: cannot import name chardet

You have a requests directory with a __init__.py . This is masking the requests package from the library , so when you try to import chardet or any such thing from requests library , it tries to search for it in this local package, and not the library one.

The best solution for this would be to rename the directory and your local package from requests to something else, so that the name does not conflict with any library packages.



来源:https://stackoverflow.com/questions/31387799/importerror-cannot-import-name-chardet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!