Importing requests in python gives error - python

I have a small python (2.7) script that works with several threads. One of the threads will read a global list and post an https post request for each one of the entries of this list.
For doing that I saw that the best way is to use python requests module. I have installed it with pip (no problem with that, it is placed in ...Python/2.7/site-packages/requests/...), but, when I import this module in my script, I get an error.
I have created another script with just one line (import requests) to reproduce the error, and I get this:
Traceback (most recent call last):
File "req.py", line 1, in <module>
import requests
File "/Library/Python/2.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/Library/Python/2.7/site-packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Python/2.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
from .exceptions import (
File "/Library/Python/2.7/site-packages/urllib3/exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import (
File "/Library/Python/2.7/site-packages/urllib3/packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/Library/Python/2.7/site-packages/urllib3/packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/Library/Python/2.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
__import__(name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 69, in <module>
from array import array
File "/Users/carlestalenssebastia/Documents/mychip/raspberry pi/array.py", line 3, in <module>
IndexError: list index out of range
Python 2.7.10
MacBook Pro with macOS Sierra (v 10.12)
Am I doing something wrong? Didn't I installed the module in the proper way?

it seems that you're having two modules with the same name array.py
rename your local array.py module to something more unique to resolve this.
take a look at this answer for more info on how to handle this issue and here to learn more on how python import system works.

Related

Numpy is Not a Generic Class

I am trying to learn Python and following along some modules. I am using Anaconda Powershell Prompt and using Python 3.7 from there. I am trying to follow along via the code below.
Everything works until I get to the line
from wrf import getvar
When I do that line I receive this
>>> from wrf import getvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Paul\anaconda3\lib\site-packages\wrf\__init__.py", line 6, in <module>
from . import api
File "C:\Users\Paul\anaconda3\lib\site-packages\wrf\api.py", line 1, in <module>
from .config import (xarray_enabled, disable_xarray, enable_xarray,
File "C:\Users\Paul\anaconda3\lib\site-packages\wrf\config.py", line 62, in <module>
_init_local()
File "C:\Users\Paul\anaconda3\lib\site-packages\wrf\config.py", line 52, in _init_local
_try_enable_xarray()
File "C:\Users\Paul\anaconda3\lib\site-packages\wrf\config.py", line 17, in _try_enable_xarray
from xarray import DataArray
File "C:\Users\Paul\anaconda3\lib\site-packages\xarray\__init__.py", line 1, in <module>
from . import testing, tutorial
File "C:\Users\Paul\anaconda3\lib\site-packages\xarray\testing.py", line 9, in <module>
from xarray.core import duck_array_ops, formatting, utils
File "C:\Users\Paul\anaconda3\lib\site-packages\xarray\core\duck_array_ops.py", line 26, in <module>
from . import dask_array_compat, dask_array_ops, dtypes, npcompat, nputils
File "C:\Users\Paul\anaconda3\lib\site-packages\xarray\core\npcompat.py", line 72, in <module>
_SupportsDType[np.dtype],
File "C:\Users\Paul\anaconda3\lib\typing.py", line 275, in inner
return func(*args, **kwds)
File "C:\Users\Paul\anaconda3\lib\typing.py", line 999, in __class_getitem__
_check_generic(cls, params, len(cls.__parameters__))
File "C:\Users\Paul\anaconda3\lib\typing.py", line 209, in _check_generic
raise TypeError(f"{cls} is not a generic class")
TypeError: <class 'numpy.typing._dtype_like._SupportsDType'> is not a generic class
Looks like the last few lines return an error but as a beginner I have zero clue what this means.
This looks like a similar error to <class 'numpy.typing._dtype_like._SupportsDType'> is not a generic class when importing the plotly.express library - an incompatibility between wrf-python and numpy.
Try upgrading those libraries to the latest versions.

Problem with importing bitcoin library in python

Today I've run into problems trying to use the python-bitcoinlib in a small python program. It worked perfectly a few days ago but today the library seems to be broken (see console output below).
I'm in a Mac with OS X 11.0.1 (yes, I upgraded today, but I would be surprised if that's the cause of the problem).
I've tried several times installing and uninstalling the library and even python, but nothing seems to happen.
Thanks in advance.
>>> from bitcoin.rpc import RawProxy
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
from bitcoin.rpc import RawProxy
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bitcoin/rpc.py", line 48, in <module>
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bitcoin/wallet.py", line 33, in <module>
import bitcoin.core.key
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/bitcoin/core/key.py", line 34, in <module>
_ssl = ctypes.cdll.LoadLibrary(ctypes.util.find_library('ssl') or 'libeay32')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py", line 452, in LoadLibrary
return self._dlltype(name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(libeay32, 6): image not found
My solution is not using RawProxy.
Impor this instead:
from bitcoinrpc.authproxy import AuthServiceProxy
And connect to bitcoin core with this sentence or equivalent:
p = AuthServiceProxy("http://%s:%s#127.0.0.1:8332"%(rpcuser, rpcpassword))

How to solve error: "numpy.dtype has the wrong size, try recompiling"

When I execute the following statement in my code on my Mac OS X
import json, nltk, requests, time, smtplib, uuid
I have the followiong error:
18/01/22 22:03:15 INFO TaskSetManager: Lost task 235.0 in stage 4.0 (TID 16) on executor localhost: org.apache.spark.api.python.PythonException (Traceback (most recent call last):
File "/usr/local/spark/python/lib/pyspark.zip/pyspark/worker.py", line 98, in main
command = pickleSer._read_with_length(infile)
File "/usr/local/spark/python/lib/pyspark.zip/pyspark/serializers.py", line 164, in _read_with_length
return self.loads(obj)
File "/usr/local/spark/python/lib/pyspark.zip/pyspark/serializers.py", line 422, in loads
return pickle.loads(obj)
File "utils.py", line 6, in <module>
import json, nltk, requests, time, smtplib, uuid
File "/Library/Python/2.7/site-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/Library/Python/2.7/site-packages/nltk/collocations.py", line 39, in <module>
from nltk.metrics import ContingencyMeasures, BigramAssocMeasures, TrigramAssocMeasures
File "/Library/Python/2.7/site-packages/nltk/metrics/__init__.py", line 16, in <module>
from nltk.metrics.scores import (accuracy, precision, recall, f_measure,
File "/Library/Python/2.7/site-packages/nltk/metrics/scores.py", line 16, in <module>
from scipy.stats.stats import betai
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/stats/__init__.py", line 324, in <module>
from .stats import *
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/stats/stats.py", line 242, in <module>
import scipy.special as special
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/special/__init__.py", line 531, in <module>
from ._ufuncs import *
File "numpy.pxd", line 155, in init scipy.special._ufuncs (scipy/special/_ufuncs.c:19983)
ValueError: numpy.dtype has the wrong size, try recompiling
As you can see from log, I'm using python 2,7 and pyspark (spark 1.6).
Maybe the problem is related to the fact that the software tries to use OSX system Python that use a specific version of numpy and I'm not able to understand how can avoid this.
Anyone can help me?
At this link: ValueError: numpy.dtype has the wrong size, try recompiling
the problem is related to the binary distribution of packages that are compiled against a recent version of numpy.
In my case it seems that spark refer the OSX system python ("/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/stats/stats.py") instead of the "/Library/Python/2.7" i'm using in my virtual env.

Python makes lib error when making FireFox Sync 1.5

I've tried to make my own server to new sync service in FireFox.
I used the following tutorial: https://docs.services.mozilla.com/howtos/run-sync-1.5.html .
When I wanted to check it by using make test I got the following error:
Traceback (most recent call last):
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/loader.py", line 403, in loadTestsFromName
module = resolve_name(addr.module)
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/nose/util.py", line 311, in resolve_name
module = __import__('.'.join(parts_copy))
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/syncstorage/__init__.py", line 5, in <module>
import mozsvc.config
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/mozsvc/config.py", line 10, in <module>
from konfig import Config, SettingsDict
File "/home/jj/syncserver/local/local/lib/python2.7/site-packages/konfig/__init__.py", line 11, in <module>
from configparser import ConfigParser, ExtendedInterpolation
ImportError: cannot import name ExtendedInterpolation
What's happened?
configparser.ExtendedInterpolation does not exist in Python 2.
You need to use Python 3.

lightblue Python module not working on mac

I'm trying to connect to wiimote via Python on mac osx 10.7.2.
For that I'm trying to use lightblue. When running: import lightblue Python gives me this error.
>>> import lightblue
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/__init__.py", line 160, in <module>
from _lightblue import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/_lightblue.py", line 27, in <module>
import _IOBluetooth
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/_IOBluetooth.py", line 47, in <module>
globals=globals())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/_bridgesupport.py", line 142, in initFrameworkWrapper
_parseBridgeSupport(data, globals, frameworkName)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/_bridgesupport.py", line 42, in _parseBridgeSupport
objc.parseBridgeSupport(data, globals, frameworkName, *args, **kwds)
ValueError: cftype for 'IOBluetoothDeviceInquiryRef' must include gettypeid_func, tollfree or both
I found one with a similar problem here: http://python.6.n6.nabble.com/Python-bridgesupport-issue-on-Lion-td2161049.html#a32196961 but the answer doesn't help me much, since it seems to me that it is already using lightblue's files. Any suggestions?
Seems like a bug with Apple's gen_bridge_metadata script. You can make the error go away by editing the generated file by hand.
Open up /System/Library/Frameworks/IOBluetooth.framework/Versions/Current/Resources/BridgeSupport/IOBluetooth.bridgesupport and delete the lines that start with <cftype.

Categories