In Python, how to begin with chardet module?

20,574

Why not try installing it using pip?

https://pip.pypa.io/en/latest/installation/

Pip is great for installing modules. Just download the get-pip.py and follow the instructions on the installation page. Once it's installed, make sure these are in your windows path: C:\Python27;C:\Python27\Tools\Scripts;C:\Python27\Scripts;

Then just run "pip install chardet" and it will install the latest version of chardet that will work with your version of python. You can use pip to install most modules without having to download the source. It's a lot easier than trying to get source files in my opinion.

Share:
20,574
user2305415
Author by

user2305415

Updated on September 24, 2021

Comments

  • user2305415
    user2305415 over 2 years

    I would like to try some code that uses the chardet module. This is the code i have found on the web :

    import urllib2
    import chardet
    
    def fetch(url):
    try:
       result = urllib2.urlopen(url)
       rawdata = result.read()
       encoding = chardet.detect(rawdata)
       return rawdata.decode(encoding['encoding'])
    
    except urllib2.URLError, e:
       handleError(e)
    

    But to try this code, i have to get the chardet module : But, i have two choices : https://pypi.python.org/pypi/chardet#downloads

    • chardet-2.2.1-py2.py3-none-any.whl (md5) Python Wheel
    • chardet-2.2.1.tar.gz (md5) Python source

    I have chosen the Python Wheel and put this file in my Python27 directory. But still does not work.

    So my problems are : - which type of chardet file to download + where to put this file for Python not to print this error : Traceback (most recent call last): File "C:/Python27/s7/test5.py", line 2, in import chardet ImportError: No module named chardet

    Note :(I'm on Python 2.7)

    Thanks in advance for any help or suggestions ! :D

    EDIT 1 : Sorry for being a very beginner, but in fact it is the python source that must be chosen! Especially, installing with setup.py, but it does not work to me ! I opened the Windows command and wrote the path to the chardet-2.2.1(unzipped) , and then i wrote : python setup.py install, but it does not work ...:S

    I think it's better to open a new subject.

  • CervEd
    CervEd about 3 years
    note the paths described depends on which version of python and where they are installed. eg. this may also be in something like ~\AppData\Local\Programs\Python\Python39\..
  • CervEd
    CervEd over 2 years
    also note you may need to run pip install chardet with admin privileges and that the install will not fail installing without. To solve uninstall and reinstall as admin