Category Archives: Python

[Solved] Importerror: DLL load failed while importing mtrand: the specified program could not be found.

Problems encountered in the early stages of OpenCV learning
ImportError: DLL load failed while importing mtrand: The specified program could not be found.
After reinstalling python, I still couldn’t use it, and then I found out many times that the problem was caused by the wrong version of python.
After trying python 3.9, python 3.6, python 3.8 and a series of other versions, I found that the same problem was still reported.

    "E:\specialize software\Python3.9\python.exe" E:/Workspace/untitled1/venv/openvtest.py
OpenCV bindings requires "numpy" package.
Install it via command:
    pip install numpy
Traceback (most recent call last):
  File "E:\Workspace\untitled1\venv\openvtest.py", line 1, in <module>
    import cv2
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\cv2\__init__.py", line 11, in <module>
    import numpy
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\numpy\__init__.py", line 161, in <module>
    from . import random
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\numpy\random\__init__.py", line 180, in <module>
    from . import _pickle
  File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\numpy\random\_pickle.py", line 1, in <module>
    from .mtrand import RandomState
ImportError: DLL load failed while importing mtrand: The specified program could not be found.

After a series of attempts, it is finally found that Anaconda 3-5.3.1 can adapt to opencv Python and numpy packages. If the problem appears, this error indicates that opencv Python and numpy have been PIP before, so I won’t repeat here. The installation methods of CV2 and numpy are everywhere. If not, please ask Du Niang.

The following is the image download website of Anaconda 3-5.3.1

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

After downloading, you can install it directly. Be careful not to install it on disk C

No error is reported after importing CV2 and numpy again using anaconda

import numpy
import cv2

if __name__ == '__main__':
    print('Success')
"E:\specialize software\Anaconda\python.exe" E:/Workspace/untitled1/venv/openvtest.py
Success

Process has ended, exit code 0

[Solved] Python Error: Attributeerror: ‘STR’ object has no attribute ‘decode’

Solution:
The above problem occurs find: Python37\lib\site-packages\django\db\backends\mysql\operations.py".

Open and modify the inside: query = query.decode(errors='replace') to query = query.encode(errors='replace') and you're done.

vim /usr/local/python3/lib/python3.6/site-packages/django/db/backends/mysql/operations.py

query = query.decode(errors='replace') Change to: query = query.encode(errors='replace')

[Solved] Python pip install Error: SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xc6

Python pip install Error: SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xc6

 

Solution:
Method 1: delete the UTF-8 code and use ASCII code

to delete all UTF-8 code contents in the script program, that is, delete all Chinese

Method 2: indicate the coding method of the script

in the python script program, actively declare that we use UTF-8 coding method

the method of declaration is as follows. Add the following sentence at the top of the program, especially the second sentence

#!/ usr/bin/python
# -*- coding: UTF-8 -*-

At first # – * – Coding: UTF-8 – * – it still reported an error when running again, and the problem of #coding = GBK was solved perfectly

Method 3: after checking, it is found that there is Chinese in the directory where the project is located, which can be changed to English.

[Solved] Python 3.8 Install pyaudio Error: pip3 install pyaudio error: Microsoft Visual C++ 14.0 or greater is required.

E:\Eprogramfiles\Anaconda3\envs\myenvpy38>pip3 install pyaudio
Collecting pyaudio
Using cached PyAudio-0.2.11.tar.gz (37 kB)
Using legacy 'setup.py install' for pyaudio, since package 'wheel' is not installed.
Installing collected packages: pyaudio
Running setup.py install for pyaudio ... error
ERROR: Command errored out with exit status 1:
command: 'c:\python38\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-m6oeci17\\pyaudio_c8557100e30446cabb941c1c6428849d\\setup.py'"'"'; __file__='"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-m6oeci17\\pyaudio_c8557100e30446cabb941c1c6428849d\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Administrator\AppData\Local\Temp\pip-record-49wse8x_\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\python38\Include\pyaudio'
cwd: C:\Users\Administrator\AppData\Local\Temp\pip-install-m6oeci17\pyaudio_c8557100e30446cabb941c1c6428849d\
Complete output (9 lines):
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.8
copying src\pyaudio.py -> build\lib.win-amd64-3.8
running build_ext
building '_portaudio' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\python38\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-m6oeci17\\pyaudio_c8557100e30446cabb941c1c6428849d\\setup.py'"'"'; __file__='"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-m6oeci17\\pyaudio_c8557100e30446cabb941c1c6428849d\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Administrator\AppData\Local\Temp\pip-record-49wse8x_\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\python38\Include\pyaudio' Check the logs for full command output.
WARNING: You are using pip version 21.1.1; however, version 21.3.1 is available.
You should consider upgrading via the 'c:\python38\python.exe -m pip install --upgrade pip' command.

========================================================
reason:
It has nothing to do with whether Visual C++ 14.0 or greater is installed.


========================================================

Solution:
Pyaudio installed directly with pip does not support 3.7, so an error will be reported during installation or import.
So directly download the whl file and install it.

whl download address:
https://github.com/intxcc/pyaudio_portaudio/releases

Switch to the whl file directory and install directly with pip

pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl

>>> import pyaudio
Could not import the PyAudio C module '_portaudio'.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\PycharmProjects\untitled\venv\lib\site-packages\pyaudio.py", line 116, in <module>
import _portaudio as pa
ModuleNotFoundError: No module named '_portaudio'

C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyaudio
Could not import the PyAudio C module '_portaudio'.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\PycharmProjects\untitled\venv\lib\site-packages\pyaudio.py", line 116, in <module>
import _portaudio as pa
ModuleNotFoundError: No module named '_portaudio'
>>>

The official PyAudio 0.2.11 does not support Python 3.7+, and trying to install results in the error C1083: Cannot open include file: 'portaudio.h'. However, there are unofficial wheels for PyAudio that support 3.7+. If you are using the official version, you must use Python 3.6 with PyAudio 0.2.11. See http://people.csail.mit.edu/hubert/pyaudio/ pip will fetch and install PyAudio wheels (prepackaged binaries). Currently, there are wheels compatible with the official distributions of Python 2.7, 3.4, 3.5, and 3.6. http://people.csail.mit.edu/hubert/pyaudio/

[Solved] Python SSL handshake failure after upgrading MacOS To Monterey

After upgrading MacOS To Monterey 12.0.1, I suddenly found that the original working Python 3 failed to request Huawei restconf API, prompting   ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1129)  , The curl provided with the MAC also fails to shake hands with the Huawei API, prompting   curl: (35) error:06FFF089:digital envelope routines:CRYPTO_ internal:bad key length  。

Libressl is used by default on the Mac platform instead of OpenSSL. SSL version information: libressl 2.8.3, curl version information: curl 7.77.0 (x86_64 Apple Darwin 21.0) libcurl/7.77.0 (securetransport) libressl/2.8.3 zlib/1.2.11 nghttp2/1.42.0.

The SSL handshake process is checked by capturing packets. It is found that curl and python 3 perform differently.

The above figure shows the handshake process of curl. You can see that the key exit has been completed, but the client actively closes the connection for some reason, and then the server reports an error.

The above figure shows the handshake interaction of Python 3 calling the http.client.httpsconnection library. You can see that the server directly reports an error just after issuing the client hello.

It is also Python 3, which is no problem on Linux, and it is no problem for Python 3 on Mac to shake hands with other HTTPS websites. Comparing the handshake capture on Linux and MAC, we can see that there are more fields in the client Hello sent by MAC. After technical communication with Huawei, he said that Huawei’s restconf API does not support tls1.3 protocol, which he believes is the reason for the handshake failure.

You can see that although SSL on Linux also supports tls1.3, it is not supported during the handshake process_ For the extension of versions, try to disable tls1.3 in Python, and the test result is that the handshake is successful.

The solution is as follows:

from ssl import _create_unverified_context
ctx = _create_unverified_context()
ctx.set_ciphers('ALL')
ctx.options &= ~ssl.OP_NO_SSLv3  #allow ssl3.0, default is forbid
ctx.options |= ssl.OP_NO_TLSv1_3  #forbid tls1.3,default is allow
opener = HTTPSConnection('xx.xx.xx.xx', port=443, timeout=3, context=ctx)

If you use urllib3 or request library, you can also modify the context here.

The above methods are supported in versions after Python 3.2. Versions before Python 3.2 can also use the following methods:

ctx = _create_unverified_context(ssl.PROTOCOL_TLSv1_2)

This constant has been removed after Python 3.6. You can only use the previous method.

Unfortunately, the problem of curl failure has not been solved. Many people are waiting for apple to fix it on the Internet.

Django: How to Convert Models object to JSON

1. When the models object uses “all()”:

from django.core import serializers 


data_set = models.Areas.objects.all()
res = serializers.serialize("json",data_set)

2. When the models object is a single value “get()”:

from django.forms.models import model_to_dict

obj = models.Areas.objects.get(id=value)
res = model_to_dict(obj)

3. The models object uses values or values_ List:

res={"status":True,"data":""}
obj = models.Areas.objects.values("id","name")
obj_list = list(obj)  #Need to convert to a list, otherwise it will report an error
res["data"] = obj_list
return JsonResponse(res,safe=False)

How to Solve Error: /usr/bin/env: ‘python’: No such file or directory

Personal notes do not guarantee success

1. Error when starting a program

/usr/bin/env: ‘python’: No such file or directory

2. Solution

1. Check the version of Python 3

python3 --version

2. Find the installation location of python3:

whereis python3

3. Create a symbolic connection for it:

sudo ln -s /usr/bin/python3 /usr/bin/python

4. Try to start again

How to Solve Wechat wxpy Robot Error

Error message sent by wechat robot of higher version Python 3.9

lib\site-packages\itchat\utils.py”, line 69, in msg_formatter

d[k]  = htmlParser.unescape(d[k])

AttributeError: ‘HTMLParser’ object has no attribute ‘unescape’

Solution: modify the source code of the corresponding utils.py file

The principle is to modify the imported package from HTML import unescape

The modified code is:

import re, os, sys, subprocess, copy, traceback, logging

from html import unescape

import requests

from . import config

logger = logging.getLogger('itchat')

emojiRegex = re.compile(r'<span class="emoji emoji(.{1,10})"></span>')
try:
    b = u'\u2588'
    sys.stdout.write(b + '\r')
    sys.stdout.flush()
except UnicodeEncodeError:
    BLOCK = 'MM'
else:
    BLOCK = b
friendInfoTemplate = {}
for k in ('UserName', 'City', 'DisplayName', 'PYQuanPin', 'RemarkPYInitial', 'Province',
        'KeyWord', 'RemarkName', 'PYInitial', 'EncryChatRoomId', 'Alias', 'Signature', 
        'NickName', 'RemarkPYQuanPin', 'HeadImgUrl'):
    friendInfoTemplate[k] = ''
for k in ('UniFriend', 'Sex', 'AppAccountFlag', 'VerifyFlag', 'ChatRoomId', 'HideInputBarFlag',
        'AttrStatus', 'SnsFlag', 'MemberCount', 'OwnerUin', 'ContactFlag', 'Uin',
        'StarFriend', 'Statues'):
    friendInfoTemplate[k] = 0
friendInfoTemplate['MemberList'] = []

def clear_screen():
    os.system('cls' if config.OS == 'Windows' else 'clear')

def emoji_formatter(d, k):
    ''' _emoji_deebugger is for bugs about emoji match caused by wechat backstage
    like :face with tears of joy: will be replaced with :cat face with tears of joy:
    '''
    def _emoji_debugger(d, k):
        s = d[k].replace('<span class="emoji emoji1f450"></span',
            '<span class="emoji emoji1f450"></span>') # fix missing bug
        def __fix_miss_match(m):
            return '<span class="emoji emoji%s"></span>' % ({
                '1f63c': '1f601', '1f639': '1f602', '1f63a': '1f603',
                '1f4ab': '1f616', '1f64d': '1f614', '1f63b': '1f60d',
                '1f63d': '1f618', '1f64e': '1f621', '1f63f': '1f622',
                }.get(m.group(1), m.group(1)))
        return emojiRegex.sub(__fix_miss_match, s)
    def _emoji_formatter(m):
        s = m.group(1)
        if len(s) == 6:
            return ('\\U%s\\U%s'%(s[:2].rjust(8, '0'), s[2:].rjust(8, '0'))
                ).encode('utf8').decode('unicode-escape', 'replace')
        elif len(s) == 10:
            return ('\\U%s\\U%s'%(s[:5].rjust(8, '0'), s[5:].rjust(8, '0'))
                ).encode('utf8').decode('unicode-escape', 'replace')
        else:
            return ('\\U%s'%m.group(1).rjust(8, '0')
                ).encode('utf8').decode('unicode-escape', 'replace')
    d[k] = _emoji_debugger(d, k)
    d[k] = emojiRegex.sub(_emoji_formatter, d[k])

def msg_formatter(d, k):
    emoji_formatter(d, k)
    d[k] = d[k].replace('<br/>', '\n')
    d[k]  = unescape(d[k])

def check_file(fileDir):
    try:
        with open(fileDir):
            pass
        return True
    except:
        return False

def print_qr(fileDir):
    if config.OS == 'Darwin':
        subprocess.call(['open', fileDir])
    elif config.OS == 'Linux':
        subprocess.call(['xdg-open', fileDir])
    else:
        os.startfile(fileDir)

def print_cmd_qr(qrText, white=BLOCK, black='  ', enableCmdQR=True):
    blockCount = int(enableCmdQR)
    if abs(blockCount) == 0:
        blockCount = 1
    white *= abs(blockCount)
    if blockCount < 0:
        white, black = black, white
    sys.stdout.write(' '*50 + '\r')
    sys.stdout.flush()
    qr = qrText.replace('0', white).replace('1', black)
    sys.stdout.write(qr)
    sys.stdout.flush()

def struct_friend_info(knownInfo):
    member = copy.deepcopy(friendInfoTemplate)
    for k, v in copy.deepcopy(knownInfo).items(): member[k] = v
    return member

def search_dict_list(l, key, value):
    ''' Search a list of dict
        * return dict with specific value & key '''
    for i in l:
        if i.get(key) == value:
            return i

def print_line(msg, oneLine = False):
    if oneLine:
        sys.stdout.write(' '*40 + '\r')
        sys.stdout.flush()
    else:
        sys.stdout.write('\n')
    sys.stdout.write(msg.encode(sys.stdin.encoding or 'utf8', 'replace'
        ).decode(sys.stdin.encoding or 'utf8', 'replace'))
    sys.stdout.flush()

def test_connect(retryTime=5):
    for i in range(retryTime):
        try:
            r = requests.get(config.BASE_URL)
            return True
        except:
            if i == retryTime - 1:
                logger.error(traceback.format_exc())
                return False

def contact_deep_copy(core, contact):
    with core.storageClass.updateLock:
        return copy.deepcopy(contact)

def get_image_postfix(data):
    data = data[:20]
    if b'GIF' in data:
        return 'gif'
    elif b'PNG' in data:
        return 'png'
    elif b'JFIF' in data:
        return 'jpg'
    return ''

def update_info_dict(oldInfoDict, newInfoDict):
    ''' only normal values will be updated here
        because newInfoDict is normal dict, so it's not necessary to consider templates
    '''
    for k, v in newInfoDict.items():
        if any((isinstance(v, t) for t in (tuple, list, dict))):
            pass # these values will be updated somewhere else
        elif oldInfoDict.get(k) is None or v not in (None, '', '0', 0):
            oldInfoDict[k] = v