Cython with Numpy / AttributeError: 'module' object has no attribute 'Handler' - python-3.x

I want to use Numpy in Cython, but encountered the following error. This error happens even if I run the simple code, so it should be an issue related to importing Numpy.
My environment:
OS X Yosemite
Python 3.4.3
setup.py:
import numpy as np
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = 'my_code',
ext_modules = cythonize('my_code.pyx'),
include_path = [numpy.get_include()]
)
my_code.pyx:
cimport numpy as np
cdef int a
Execute in Terminal:
$ python3 setup.py build_ext --inplace
Traceback (most recent call last):
File "/Users/***/setup.py", line 1, in <module>
import numpy as np
File "/usr/local/lib/python3.4/site-packages/numpy/__init__.py", line 180, in <module>
from . import add_newdocs
File "/usr/local/lib/python3.4/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python3.4/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/usr/local/lib/python3.4/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/usr/local/lib/python3.4/site-packages/numpy/core/__init__.py", line 58, in <module>
from numpy.testing import Tester
File "/usr/local/lib/python3.4/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/__init__.py", line 59, in <module>
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 253, in <module>
class _CapturingHandler(logging.Handler):
AttributeError: 'module' object has no attribute 'Handler'

Old question but I ran into this because I had a very similar issue with numpy.
It turns out I had a directory named logging as a sibling of the script I was trying to run. So the problem was a simple naming collision between my local project folder and the logging module numpy expected to find. Renaming the project logging folder solved the issue for me.
Looking at OP's error message, I suspect this was the case for them as well.

Related

Python3: ImportError: cannot import name 'InvalidProxiesConfigError' from 'botocore.httpsession'

My use case is that I am trying to write my dataframe to S3 bucket for which I installed s3fs==2015.5.0 using pip3. Now when I run the code
import s3fs
def my_func():
# my logic
my_func()
It returns the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import s3fs
File "/usr/local/lib/python3.7/dist-packages/s3fs/__init__.py", line 1, in <module>
from .core import S3FileSystem, S3File
File "/usr/local/lib/python3.7/dist-packages/s3fs/core.py", line 14, in <module>
import aiobotocore
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/__init__.py", line 11, in <module>
from .session import get_session, AioSession
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/session.py", line 6, in <module>
from .client import AioClientCreator, AioBaseClient
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/client.py", line 11, in <module>
from .args import AioClientArgsCreator
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/args.py", line 8, in <module>
from .endpoint import AioEndpointCreator
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/endpoint.py", line 12, in <module>
from aiobotocore.httpsession import AIOHTTPSession
File "/usr/local/lib/python3.7/dist-packages/aiobotocore/httpsession.py", line 12, in <module>
from botocore.httpsession import ProxyConfiguration, create_urllib3_context, \
ImportError: cannot import name 'InvalidProxiesConfigError' from 'botocore.httpsession' (/usr/local/lib/python3.7/dist-packages/botocore/httpsession.py)
My requirements.txt file something like this:
boto3==1.18.65
s3fs==2021.5.0
fastparquet==0.7.1
pip3 install -r requirements.txt install the packages but also returns
aiobotocore 1.4.2 requires botocore<1.20.107,>=1.20.106, but you'll have botocore 1.19.28 which is incompatible.
s3fs 2021.5.0 requires fsspec==2021.05.0, but you'll have fsspec 2021.10.1 which is incompatible.
For some reason it just started to happen now. I was using the similar packages earlier and it was working fine. Any ideas why it is not working now?

Error while importing load_model from keras.model

I am trying to import my keras model which I have saved as .h5 file. But the very first line of my import gives me error. This is the line which gives error
from keras.models import load_model
I checked all the dependencies and that is ok. I have Keras 2.2.4. Error that I am getting:
Using TensorFlow backend.
Traceback (most recent call last):
File "drive1.py", line 1, in <module>
from keras.models import load_model
File "E:\python3.6.6\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "E:\python3.6.6\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
from . import conv_utils
File "E:\python3.6.6\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
from .. import backend as K
File "E:\python3.6.6\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
from .tensorflow_backend import *
File "E:\python3.6.6\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
import tensorflow as tf
File "C:\Users\Dhruv\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import * # pylint: disable=redefined-builtin
File "C:\Users\Dhruv\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "C:\Users\Dhruv\AppData\Roaming\Python\Python36\site-packages\tensorflow\core\framework\graph_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
File "E:\python3.6.6\lib\site-packages\google\protobuf\__init__.py", line 37, in <module>
__import__('pkg_resources').declare_namespace(__name__)
File "E:\python3.6.6\lib\site-packages\pkg_resources.py", line 1479, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
I feel like there is some package versions mismatch but not sure what should I check further. I am using pip for installing packages.
Finally from some reference I found that there was some version mismatch between my python and pip. Actually I was should have focused on following error message:
__import__('pkg_resources').declare_namespace(__name__)
And the thing which helped was
python -m ensurepip --upgrade

ImportError: cannot import name '_ni_support' -- Using cx-freeze with scipy

I have used cx-freeze to make an executable file from my python 3 script. The issue is that apparently cx-freeze is having a hard time importing scipy scripts. I had to resolve other issues previously, e.g. adding the tcl library files manually. Anyway, my setup files is below:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\\Continuum\\anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\Gobryas\\AppData\\Local\\Continuum\\anaconda3\\tcl\\tk8.6"
additional_mods = ['numpy.core._methods', 'numpy.lib.format']
setup(name = "Curve Characterization" ,
options = {'build_exe': {'includes': additional_mods}},
version = "0.1" ,
description = "" ,
executables = [Executable("curvCharLite.py")])
This is the error that I get:
ImportError: cannot import name '_ni_support'
Full details
Traceback (most recent call last):
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "curvCharLite.py", line 21, in <module>
File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\__init__.py", line 8, in <module>
from .detection import (Binseg, BottomUp, Dynp, Omp, OmpK, Pelt, Window,
File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\detection\__init__.py", line 51, in <module>
from .window import Window
File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\detection\window.py", line 112, in <module>
from scipy.signal import argrelmax
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\signal\__init__.py", line 311, in <module>
from ._savitzky_golay import savgol_coeffs, savgol_filter
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\signal\_savitzky_golay.py", line 6, in <module>
from scipy.ndimage import convolve1d
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\ndimage\__init__.py", line 161, in <module>
from .filters import *
File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\ndimage\filters.py", line 35, in <module>
from . import _ni_support
ImportError: cannot import name '_ni_support'
PS 1: There is a similar issue asked here, but no helpful answer really.
PS 2: I'm using scipy 1.1.0, cx-freeze 5.1.1, and python 3.6.5
So, I think I found a solution for this problem inspired by the post by #fepzzz.
Looks like there are bad conflicts between cx-freeze and scipy which can be avoided as follows. I needed to modify the include_files build option as below:
import os
import scipy
includefiles_list=[]
scipy_path = os.path.dirname(scipy.__file__)
includefiles_list.append(scipy_path)
build_options = dict(packages=['matplotlib'], #this line solves an issue w/ matplotlib
include_files=includefiles_list, #this line is for scipy issue
includes=['matplotlib.backends.backend_qt5agg']) #this line solves another issue w/ matplotlib
Hope this helps others.

I am getting this error "No module named 'darkflow.cython_utils.cy_yolo_findboxes'" When I am using darknet

When I try to use Pycharm to play with YOLO, I got the error.
Here is what I got, Any help will be appreciated.
Node: I have done python3 setup.py build_ext --inplace. All the file like cy_yolo_findboxes.c and cy_yolo2_findboxes are all inside the cython_utils folder. But it does not work.
import cv2
import sys
sys.path.append('/Users/hantaoliu/darkflow-master')
import tensorflow as tf
from darkflow.net.build import TFNet
import numpy as np
import time
option = {
'model': 'cfg/yolo.cfg',
'load': 'bin/yolo.weights',
'threshold': 0.15,
'gpu': 1.0
}
capture = cv2.VideoCapture('videofile1.mp4')
colors =[tuple(255 * np.random(3)) for i in range(5)]
for color in colors:
print(color)
Here are the error message
Traceback (most recent call last):
File "/Users/hantaoliu/PycharmProjects/YOLO/sample.py", line 5, in <module>
from darkflow.net.build import TFNet
File "/Users/hantaoliu/darkflow-master/darkflow/net/build.py", line 7, in <module>
from .framework import create_framework
File "/Users/hantaoliu/darkflow-master/darkflow/net/framework.py", line 1, in <module>
from . import yolo
File "/Users/hantaoliu/darkflow-master/darkflow/net/yolo/__init__.py", line 2, in <module>
from . import predict
File "/Users/hantaoliu/darkflow-master/darkflow/net/yolo/predict.py", line 7, in <module>
from ...cython_utils.cy_yolo_findboxes import yolo_box_constructor
ImportError: No module named 'darkflow.cython_utils.cy_yolo_findboxes'
Build the cython module
cd ./cython_utils
python3 setup.py build_ext --inplace

Python Sklearn import error

I'm new to machine learning libraries in python. I've installed 'python 3.4'.
I've also installed scikit-learn , numpy and scipy from wheel files.
scikit_learn-0.18.1-cp34-none-win_amd64
scipy-0.16.0-cp34-none-win_amd64
numpy-1.9.2-cp34-none-win_amd64.whl
import scipy
import numpy
above statements are working fine. but when i try to execute import sklearn
i get below error
Traceback (most recent call last):
File "C:\Users\Usman\Desktop\hello_python.py", line 1, in <module>
import sklearn
File "C:\Python34\Lib\site-packages\sklearn\__init__.py", line 57, in <module>
from .base import clone
File "C:\Python34\Lib\site-packages\sklearn\base.py", line 12, in <module>
from .utils.fixes import signature
File "C:\Python34\Lib\site-packages\sklearn\utils\__init__.py", line 10, in <module>
from .murmurhash import murmurhash3_32
File "__init__.pxd", line 155, in init sklearn.utils.murmurhash (sklearn\utils\murmurhash.c:6319)
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96
--edit
when i execute import sklearn from python shell i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\Lib\site-packages\sklearn\__init__.py", line 56, in <module>
from . import __check_build
ImportError: cannot import name '__check_build'
I've searched but couldn't find anything helpful. Kindly help me remove this error.

Resources