Category Archives: Python

Python Error: certificate verify failed: certificate has expired

Let’s talk about the situation at that time. Because there is no cifar10 data set on this machine, when downloading cifar10 with the following code:

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)

Certificate verify failed: the error message certificate has expired appears.

Later, I found that the following two lines can be added to the file header:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

Done! happy

Pandas uses str.contains to filter error [How to Solve]

When using pandas to filter excel,

df.loc[df['threat_type'].str.contains("DGA")]

The following error messages appear:

ValueError: Cannot mask with non-boolean array containing NA/NaN values

It is reported that the grouping column contains non-string contents. Because the use of .Str.contains requires that the field must be a string and cannot have numbers,
so it is added to the code

df.loc[df['threat_type'].str.contains("DGA", na=False)]

This enables the function to directly ignore non-string situations.

[Solved] sys.stderr.write(f“ERROR: {exc}“) ^ SyntaxError: invalid syntax

Reference link: (19 messages) two reasons and solutions for syntaxerror: invalid syntax in PIP instruction,

The relevant solutions in the original text are as follows:

However, the get-pip.py file was not found in the process of trying to solve it. Access the index of /.

https://bootstrap.pypa.io/ Observe that the relevant version of get-pip.py file is found in PIP

To sum up, the command to get-pip.py should be

wget https://bootstrap.pypa.io/pip/3.5/get-pip.py

After downloading, execute the following command
python3 get-pip.py

Mac install python_Levenshtein Error: error: command ‘clang‘ failed with exit status 1

mac install python_Levenshtein error: error: command ‘clang’ failed with exit status

1. Environment
Intel x86_64
macOS 10.15.7
2. the error report content

Running setup.py install for python-Levenshtein … error
ERROR: Command errored out with exit status 1:
command: /Library/Developer/CommandLineTools/usr/bin/python3 -u -c ‘import io, os, sys, setuptools, tokenize; sys.argv[0] = ‘”’”’/private/var/folders/03/qf7t38k94gq5y14qdcq95kl00000gp/T/pip-install-8w35qr01/python-levenshtein_7341c7b5016e45e290c12556bee6b632/setup.py’”’”’; file=’”’”’/private/var/folders/03/qf7t38k94gq5y14qdcq95kl00000gp/T/pip-install-8w35qr01/python-levenshtein_7341c7b5016e45e290c12556bee6b632/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 /private/var/folders/03/qf7t38k94gq5y14qdcq95kl00000gp/T/pip-record-420z5p0i/install-record.txt –single-version-externally-managed –user –prefix= –compile –install-headers /Users/linzheng/Library/Python/3.8/include/python3.8/python-Levenshtein
cwd: /private/var/folders/03/qf7t38k94gq5y14qdcq95kl00000gp/T/pip-install-8w35qr01/python-levenshtein_7341c7b5016e45e290c12556bee6b632/
Complete output (128 lines):
running install
running build
running build_py
creating build
creating build/lib.macosx-10.14.6-x86_64-3.8
creating build/lib.macosx-10.14.6-x86_64-3.8/Levenshtein
copying Levenshtein/StringMatcher.py -> build/lib.macosx-10.14.6-x86_64-3.8/Levenshtein
copying Levenshtein/init.py -> build/lib.macosx-10.14.6-x86_64-3.8/Levenshtein
running egg_info
writing python_Levenshtein.egg-info/PKG-INFO
writing dependency_links to python_Levenshtein.egg-info/dependency_links.txt
writing entry points to python_Levenshtein.egg-info/entry_points.txt
writing namespace_packages to python_Levenshtein.egg-info/namespace_packages.txt
writing requirements to python_Levenshtein.egg-info/requires.txt
writing top-level names to python_Levenshtein.egg-info/top_level.txt
reading manifest file ‘python_Levenshtein.egg-info/SOURCES.txt’
reading manifest template ‘MANIFEST.in’
warning: no previously-included files matching ‘pyc’ found anywhere in distribution
warning: no previously-included files matching ‘so’ found anywhere in distribution
warning: no previously-included files matching ‘.project’ found anywhere in distribution
warning: no previously-included files matching ‘.pydevproject’ found anywhere in distribution
writing manifest file ‘python_Levenshtein.egg-info/SOURCES.txt’
copying Levenshtein/_levenshtein.c -> build/lib.macosx-10.14.6-x86_64-3.8/Levenshtein
copying Levenshtein/_levenshtein.h -> build/lib.macosx-10.14.6-x86_64-3.8/Levenshtein
running build_ext
building ‘Levenshtein._levenshtein’ extension
creating build/temp.macosx-10.14.6-x86_64-3.8
creating build/temp.macosx-10.14.6-x86_64-3.8/Levenshtein
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c Levenshtein/_levenshtein.c -o build/temp.macosx-10.14.6-x86_64-3.8/Levenshtein/_levenshtein.o
In file included from Levenshtein/_levenshtein.c:99:
In file included from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/Python.h:11:
In file included from /Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include/limits.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/limits.h:63:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
#error Unsupported architecture
^
In file included from Levenshtein/_levenshtein.c:99:
In file included from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/Python.h:11:
In file included from /Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include/limits.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/limits.h:64:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/machine/limits.h:8:2: error: architecture not supported
#error architecture not supported
^
In file included from Levenshtein/_levenshtein.c:99:
In file included from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/Python.h:25:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdio.h:64:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_stdio.h:71:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types.h:27:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:33:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/machine/_types.h:34:2: error: architecture not supported
#error architecture not supported
^
In file included from Levenshtein/_levenshtein.c:99:
In file included from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/Python.h:25:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdio.h:64:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_stdio.h:71:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/_types.h:27:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:55:9: error: unknown type name ‘__int64_t’
typedef __int64_t __darwin_blkcnt_t;/total blocks /
^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:56:9: error: unknown type name ‘__int32_t’; did you mean ‘__int128_t’?
typedef __int32_t __darwin_blksize_t;/preferred block size /
^
note: ‘__int128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:57:9: error: unknown type name ‘__int32_t’; did you mean ‘__int128_t’?
typedef __int32_t __darwin_dev_t;/dev_t /
^
note: ‘__int128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:60:9: error: unknown type name ‘__uint32_t’; did you mean ‘__uint128_t’?
typedef __uint32_t __darwin_gid_t;/[???] process and group IDs /
^
note: ‘__uint128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:61:9: error: unknown type name ‘__uint32_t’; did you mean ‘__uint128_t’?
typedef __uint32_t __darwin_id_t;/[XSI] pid_t, uid_t, or gid_t/
^
note: ‘__uint128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:62:9: error: unknown type name ‘__uint64_t’
typedef __uint64_t __darwin_ino64_t; /* [???] Used for 64 bit inodes /
^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:68:9: error: unknown type name ‘__darwin_natural_t’
typedef __darwin_natural_t __darwin_mach_port_name_t;/Used by mach /
^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:70:9: error: unknown type name ‘__uint16_t’; did you mean ‘__uint128_t’?
typedef __uint16_t __darwin_mode_t;/[???] Some file attributes /
^
note: ‘__uint128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:71:9: error: unknown type name ‘__int64_t’
typedef __int64_t __darwin_off_t;/[???] Used for file sizes /
^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:72:9: error: unknown type name ‘__int32_t’; did you mean ‘__int128_t’?
typedef __int32_t __darwin_pid_t;/[???] process and group IDs /
^
note: ‘__int128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:73:9: error: unknown type name ‘__uint32_t’; did you mean ‘__uint128_t’?
typedef __uint32_t __darwin_sigset_t;/[???] signal set /
^
note: ‘__uint128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:74:9: error: unknown type name ‘__int32_t’; did you mean ‘__int128_t’?
typedef __int32_t __darwin_suseconds_t;/[???] microseconds /
^
note: ‘__int128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:75:9: error: unknown type name ‘__uint32_t’; did you mean ‘__uint128_t’?
typedef __uint32_t __darwin_uid_t;/[???] user IDs /
^
note: ‘__uint128_t’ declared here
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types.h:76:9: error: unknown type name ‘__uint32_t’; did you mean ‘__uint128_t’?
typedef __uint32_t __darwin_useconds_t;/[???] microseconds */
^
note: ‘__uint128_t’ declared here
In file included from Levenshtein/_levenshtein.c:99:
In file included from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/Py

[Solved] Selenium Error: ERROR: Message: element not interactable

For this error, you can check whether there are duplicate elements on the next page

For example, the following: run the error on the old newspaper and find that there are duplicate elements on the page

password=(By.XPATH,'//input[@placeholder="Please input the password"])'

Solution: find the one you need, as follows

password=(By.XPATH,'(//input[@placeholder="Please input the password"])[2]')

Specify element positioning method: (//span [text() = ‘match’) [2]

Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict

When the model is saved, it is saved with key pairs. At the same time, when loading, find the key value corresponding to the model according to the key value of the current network, and then load it. Generally, an error is reported because the key values of the model and the network do not match.

1. The most common problem is that there are too many key values or less module

In this case, the key value saved by the model after dataparallel or DDP training has module  , The key value of the corresponding network has no module

1) You can:

model = nn.DataParallel(model)

Add the key value of the model to module

2) You can also modify the key value by traversing the key pair value of the model.

For example, delete redundant modules when loading models   The code is as follows

state_dict = torch.load(load_path)
for key, param in state_dict.items():
    if key.startswith('module.'):        
        state_dict[key[7:]] = param          
        state_dict.pop(key)
net.load_state_dict(state_dict)
        

2. Explain load in detail_state_False parameter of dict (state_dict, false)

Many tutorials say that if the names do not match, you can directly add the false parameter, but you need to pay attention to a big pit here.

If the key value of the model does not match the key value of the network, the model will not load the pre training parameters, although no error will be reported.

The false parameter is used to the non-strict matching loading model can be analyzed in the following cases.

1) The model contains some parameters of the network

For example, the model is resnet101, and your current network is resnet50. Assuming that the parameter name of resnet50 is included in the parameters of resnet101, using false directly will load parameters with the same key value for your network resnet50. This avoids circular matching of each key pair value of resnet101 to see if it is required by resnet50.

2) The model does not contain the parameters of the network at all

As shown in case 1, the model has 100 parameters, all of which contain ‘module.’, and the network also has 100 parameters, all of which do not have ‘module.’. In this case, if the parameter is set to false, it will be found that no key-value can match, so the network will not load any parameters.

3) Introduce another false usage scenario

For example, in the distillation network pisr, the teacher network includes encoder and decoder, and the student network is composed of decoder. Therefore, when training the student network, if you want to load the pre-training model saved by the teacher network, setting false will automatically identify that the key values of the decoder are the same, and then load it.

To sum up, after setting the false parameter, the parameters are still loaded according to the key value. How many key values match, how many model parameters are loaded.

3. As long as the parameter size is the same, it can be loaded

For example, I have a 10 layer network model and a 3-layer network. I want to load the parameters of layer 9 into layer 1 of the current network. If the parameters have the same size, you can traverse the key pair values. Load the parameter into the desired key value.

state_dict = torch.load(load_path)
new_state_dict = []
for key, param in state_dict.items():
    if 'conv9' in key:        
        new_state_dict[key.replace('conv9', 'conv1')] = param   
net.load_state_dict(new_state_dict)

Syntaxerror: unexpected EOF while parsing solution and ideas

Online learning code, error reporting

SyntaxError: unexpected EOF while parsing

If you encounter this problem, first check whether the parentheses are written less

View source code

res = sorted(res.iteritems(), key=lambda x : x[1])

The code is the version of python2. X. res.iteritems () has been dropped under python3. X, so it is modified to

res = sorted(res.items(), key=lambda x : x[1])

Pyinstaller Error: [ImportError]: DLL load failed while importing _socket: Parameter error

Convert the python program to exe through pyinstaller and run normally on this computer, but an error is reported when running on other computers:
[importerror]: DLL load failed while importing_ Socket: parameter error
as shown in the following figure:

found many places without solutions

Finally, only the pyinstaller is reduced to 3.6, and the python version is also reduced to 3.6.8

Everything is normal after re-conversion!

[Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed

1. Question.
Python3.8 environment
When using pip3 to install opencv-python, there is a
[modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all’ failed

2. Cause.
When compiling in opencv-python, it cannot call python3.8 properly
3. Solution:
sudo apt-get install python3.8-dev
4. Successful installation

[Solved] cv2.error: (-215:Assertion failed) encoder->isFormatSupported(CV_8U) in function ‘imwrite_‘

OpenCV Error:
cv2.error: OpenCV(4.5.1) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-39p1qqfs/opencv/modules/imgcodecs/src/loadsave.cpp:694: error: (-215:Assertion failed) encoder->isFormatSupported(CV_8U) in function ‘imwrite_’

    1. First check if the path is wrong check the image type, for example, for exr suffix images, float64 can not be saved

Solution:

img = img.astype(np.float32)

[Solved] Python opencv imshow Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow

The result of error reporting is believed to be the same:

There is no Chinese in the path, but this error occurs. It feels very outrageous.

I couldn’t find the problem. Later, I wrote all the paths of the picture and found it\ F is a string formatting code. People are stupid, so friends write all the paths and find the problem.

This is the modified path:

In fact, when you see the formatting code, python will display it in another color, so I thought of it here! The path cannot be found until it is written!