ImportError: cannot import name chardet - python

Hi i have written a python scraper 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=['CompanyScraper-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 "CompanyScraper-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.

As you can see from the traceback -
Traceback (most recent call last):
File "CompanyScraper-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.

Related

pip3 not installing in correct folder

While python3 expects modules to be in /usr/local/lib/python3.4, pip3 installs them in /usr/local/lib/python3.4/dist-packages. This results in python3 not finding packages that pip3 says are installed.
I tried cutting and pasting the stuff in /usr/local/lib/python3.4/dist-packages to /usr/local/lib/python3.4, but I end up with weird errors, like this:
Traceback (most recent call last):
File "setup.py", line 13, in <module>
from Cython.Build import cythonize
File "/usr/local/lib/python3.4/Cython/Build/__init__.py", line 1, in <module>
from .Dependencies import cythonize
File "/usr/local/lib/python3.4/Cython/Build/Dependencies.py", line 51, in <module>
from ..Compiler.Main import Context, CompilationOptions, default_options
File "/usr/local/lib/python3.4/Cython/Compiler/Main.py", line 30, in <module>
from .Symtab import ModuleScope
File "/usr/local/lib/python3.4/Cython/Compiler/Symtab.py", line 18, in <module>
from . import PyrexTypes
File "/usr/local/lib/python3.4/Cython/Compiler/PyrexTypes.py", line 17, in <module>
from .Code import UtilityCode, LazyUtilityCode, TempitaUtilityCode
ImportError: /usr/local/lib/python3.4/Cython/Compiler/Code.cpython-34m.so: undefined symbol: PyFPE_jbuf
What do I do now? I'm on Linux Mint Cinnamon 64-bit.
[EDIT]
This problem was a complete mess and I still have no clue what happened before. I have given up trying to get Python to work, as I have found an application that does the same thing as the Python application I was trying to get working. It's such a pain.

ImportError: No module named 'queue' while running my app freezed with cx_freeze

I am using python 3.4. I am able to run my python script without any problem.
But While running my freezed python script , following error have appeared.
I am able to freeze my script successfully too with cx_freeze.
C:\Program Files (x86)\utils>utils.exe
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\requests\packages\__init__.py", line 27, i
n <module>
from . import urllib3
File "C:\Python34\lib\site-packages\requests\packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 28, in <module>
from .packages.six.moves.queue import LifoQueue, Empty, Full
File "C:\Python34\lib\site-packages\requests\packages\urllib3\packages\six.py", line 203, in load_module
mod = mod._resolve()
File "C:\Python34\lib\site-packages\requests\packages\urllib3\packages\six.py", line 115, in _resolve
return _import_module(self.mod)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
ImportError: No module named 'queue'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 12, in <module>
__import__(name + "__init__")
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 21, in <module>
scriptModule = __import__(moduleName)
File "utils.py", line 3, in <module>
File "C:\Python34\lib\site-packages\requests\__init__.py", line 63, in <module>
from . import utils
File "C:\Python34\lib\site-packages\requests\utils.py", line 24, in <module>
from ._internal_utils import to_native_string
File "C:\Python34\lib\site-packages\requests\_internal_utils.py", line 11, in <module>
from .compat import is_py2, builtin_str
File "C:\Python34\lib\site-packages\requests\compat.py", line 11, in <module>
from .packages import chardet
File "C:\Python34\lib\site-packages\requests\packages\__init__.py", line 29, in <module>
import urllib3
File "C:\Python34\lib\site-packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Python34\lib\site-packages\urllib3\connectionpool.py", line 28, in <module>
from .packages.six.moves.queue import LifoQueue, Empty, Full
File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 203, in load_module
mod = mod._resolve()
File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 115, in _resolve
return _import_module(self.mod)
File "C:\Python34\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
ImportError: No module named 'queue'
Even tried installing package 'six' with no help.
My setup.py is
from cx_Freeze import setup, Executable
import requests.certs
setup(
name = "utils" ,
version = "0.1" ,
description = " utils for accounts" ,
executables = [Executable("utils.py")],
options = {"build_exe": {"packages": ["urllib", "requests"],"include_files":[(requests.certs.where(),'cacert.pem')]}},
)
script imports following module
import requests
import urllib.request
import uuid
import json
import http.client
from xml.dom import minidom
Any help will be highly appreciated. please see me as novice in python
I had the same issues running on Ubuntu with Python 3.5. It seems that cx_freeze has problems with libraries that import other files or something like that.
Importing Queue together with requests worked for me, so:
import requests
from multiprocessing import Queue
And I don't think specifying urllib in "packages": ["urllib", "requests"] is necessary.
There are Several options based on project packages:
Method1:
Answer: I solve the problem my issue was I had file named queue.py in the same
directory
Method2:
Queue is in the multiprocessing module so:
from multiprocessing import Queue
Method3:
Updating pip from 1.5.6 to 8.1.2
`sudo python -m pip install -U pip`
Reboot system (don't know if necessary, but only after reboot new version of pip was listed)
Method4:
from six.moves.queue import Queue //I don't know how u import six package
In setup.py, options={"build_exe": {"packages": ["multiprocessing"]}} can also do the trick.
In addition to
from multiprocessing import Queue
I rolled back to the older version of cx_freeze:
pip install cx-freeze==4.3.3
Besides, the "requests" library complained on absence of "urllib3" module. I upgraded this to requests==2.13.0 and all now works.
I'm using Python 3.4 on Win10. Hope this will help.

Getting error when importing some package on python scripts

Here is my error:
Traceback (most recent call last):
File "./convert.py", line 6, in <module>
import openpyxl # from https://pythonhosted.org/openpyxl/ or PyPI (e.g. via pip)
ImportError: No module named openpyxl
i have installed the python package openpyxl but this error still appear
Im using linux python version 2.6.6
any help please
i got this new error when i ran the python2.7 script.py:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in <module>
from pip import main
File "/usr/local/lib/python2.7/site-packages/pip/__init__.py", line 13, in <module>
from pip.utils import get_installed_distributions, get_prog
File "/usr/local/lib/python2.7/site-packages/pip/utils/__init__.py", line 15, in <module>
import zipfile
File "/usr/local/lib/python2.7/zipfile.py", line 6, in <module>
import io
File "/usr/local/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: /usr/local/lib/python2.7/lib-dynload/_io.so: undefined symbol: _PyErr_ReplaceException
openpyxl module has 2 dependent modules : 1. jdcal 2. et_xmlfile
I was able to install openpyxl module and this is what I did :
1.Downloaded the openpyxl,jdcal and et_xmlfile from https://pypi.python.org/pypi and saved jdcal-1.0.tar.gz, et_xmlfile-1.0.0.tar.gz,openpyxl-2.3.0-b2.tar.gz in a local folder in my system.
2.Then I ran the commands in the following order :
pip install jdcal-1.0.tar.gz
pip install et_xmlfile-1.0.0.tar.gz
pip install openpyxl-2.3.0-b2.tar.gz
openpyxl got successfully after this. Please check whether this helps.

py2neo offline install failing

I'm trying to install py2neo on an offline machine. I can't use pip because I'm not connected to the internet. I'm trying to install py2neo-2.0.3 with python2.7.9 on redhat. When I run python setup.py install I get ImportError: cannot import name HTTPSConnection.
Traceback (most recent call last):
File "setup.py", line 29, in <module>
from py2neo import __author__, __email__, __license__, __package__, __version__
File "usr/lib/py2neo-2.0.3/py2neo/__init__.py" line 27, in <module>
from py2neo.core import *
File "usr/lib/py2neo-2.0.3/py2neo/core.py", line 28, in <module>
from py2neo.env import NEO4J_AUTH_TOKEN, NEO4J_URI
File "usr/lib/py2neo-2.0.3/py2neo/env.py", line 21, in <module>
from py2neo.packages.httpstream.packages.urimagic import URI
File "usr/lib/py2neo-2.0.3/py2neo/packages/httpstream/__init__.py", line 32, in <module>
from .http import *
File "usr/lib/py2neo-2.0.3/py2neo/packages/httpstream/http.py", line 31, in <module>
from httplib import (BadStatusLine, CannotSendRequest,
ImportError: cannot import name HTTPSConnection
I've tried installing httpstream but that didn't fix the import error.
HTTPSConnection should come with the standard library and isn't part of py2neo itself:
https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection
I don't know specifically about how RedHat package Python but it's possible you need to install SSL support in addition to the main language. I believe this is a separate compilation option:
http://www.webtop.com.au/blog/compiling-python-with-ssl-support-fedora-10-2009020237

ImportError: cannot import name iocpsupport

I am using twisted API and getting following error
Traceback (most recent call last):
File "C:\Documents and Settings\ggne0622\Desktop\Python\google-python-exercises\babynames\SimpleAgent.py", line 16, in <module>
from twisted.internet.iocpreactor import reactor
File "C:\Python27\Lib\site-packages\twisted\internet\iocpreactor\__init__.py", line 8, in <module>
from twisted.internet.iocpreactor.reactor import install
File "C:\Python27\Lib\site-packages\twisted\internet\iocpreactor\reactor.py", line 17, in <module>
from twisted.internet.iocpreactor import iocpsupport as _iocp
ImportError: cannot import name iocpsupport
It looks like you installed Twisted without building the IOCP extensions. Either build them (requires a version of MSVC compatible with your Python installation) or use a binary installer - eg one of the several on https://pypi.python.org/pypi/Twisted

Categories