Category Archives: Python

The solution of cannot import name mlpclassifier when using sklearn

 1 scikit-learn v0.17 has only BernoulliRBM, no MLPClassifier.
 2 
 3 Just upgrade scikit-learn to v0.18.
 4 
 5 Just enter any of the following commands in the console.
 6 
 7 conda update scikit-learn
 8 
 9 pip install --upgrade scikit-learn
10 
11 pip install scikit-learn==0.18.rc2
12 
13 pip install git+https://github.com/scikit-learn/scikit-learn.git

python-docx-template: Jinja’builtin_function_or_method’ object is not iterable

error:

File “C:\Python27\lib\site-packages\jinja2\environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “C:\Python27\lib\site-packages\jinja2\environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “<template>”, line 1, in top-level template code
TypeError: ‘builtin_function_or_method’ object is not iterable

Check that the keywords values etc. are used in the template references

{%tc for col in report.threat_score.weeks %} {{col}} {%tc endfor %}
{%tc for col in report.threat_score.values %} {{col}} {%tc endfor %}
{%tc for col in report.threat_score.values %}

[Solved] Typeerror: incorrect padding occurred in python3 Base64 decoding

Today, when solving the crawler’s analysis of encryption parameters, we need to use Base64 decoding. However, there is a type error: incorrect padding error in the process. Here is the solution for easy reference

In fact, the normal use of Base64 is not a problem, such as the following code

 1 #!usr/bin/env python
 2 # coding:utf-8
 3 
 4 import base64
 5 
 6 a = b'hello'
 7 b = base64.b64encode(a)
 8 # Base64 encoding of a
 9 print(b) # b'aGVsbG8='
10 # base64 decoding of b, that is, the decoding of a encoded content
11 c = base64.b64decode(b)
12 print(c)  # b'hello'

The coding result of the above code is complete, so there is no problem in decoding it directly. If the encoding result is incomplete, for example, if the value of the given bytes object in the above code is b’agvsbg8 ‘, an exception prompt of typeerror: incorrect padding will appear. For example, the following code is wrong

#!usr/bin/env python
# coding:utf-8

import base64

a = b'aGVsbG8'
b = base64.b64decode(a)
print(b)  #binascii.Error: Incorrect padding

The solution is as follows:

 1 #!usr/bin/env python
 2 # coding:utf-8
 3 
 4 import base64
 5 
 6 a = b'aGVsbG8'
 7 missing_padding = 4 - len(a) % 4
 8 if missing_padding:
 9     a += b'=' * missing_padding
10 b = base64.b64decode(a)
11 print(b) # b'hello'

In this way, the problem is solved. In fact, the equal sign is added after it. And missing_ Padding calculates the number of equal signs. If you figure out the number of = signs, it’s OK to add them directly. For example, the following code:

1 #!usr/bin/env python
2 # coding:utf-8
3 
4 import base64
5 import chardet
6 
7 a = b'aGVsbG8'
8 c = base64.b64decode(a + b'=')
9 print(c) # b'hello'

As for how to calculate, we need to understand the principle of Base64. In an equation, 3×8 = 4×6, that is, those who used to be able to store 3 bytes can now store 4 bytes, but the original bit is divided, and each byte is represented by 6 bits. Because each byte after segmentation has only 6 bits, the less than two bits are filled with 0. Moreover, these four bytes can be regarded as a whole. After Base64 decoding, the length of bytes is at least 4 and a multiple of 4, and the insufficient parts are filled with ‘=’

Confused?In fact, my expression is not good, and I don’t want to draw. Or look at the code:

 1 #!usr/bin/env python
 2 # coding:utf-8
 3 
 4 import base64
 5 
 6 # Original 1x8 = 8 bits
 7 a = b'h'
 8 # after base64 encoding 8/6 = 1 remainder 2, so at least 2 byte bits are needed, in order to meet the divisible by 4, you need to add two = signs
 9 b = base64.b64encode(a)
10 print(b) # b'aA=='
11 
12 # Process the encoded result by removing the '=' sign
13 c = b.decode('utf-8').rstrip('=')
14 # decode the result, the previous has calculated the need for 2 = sign, directly add it is good
15 d = base64.b64decode(c.encode('utf-8') + b'=' * 2)
16 # Restore the result
17 print(d)  # b'h'

This is clear at a glance, and that’s all for the summary of Base64 exceptions

Mac: Python Install lxml Error: Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed

Failed to install lxml

#include “libxml/xpath.h”
^
1 error generated.
*********************************************************************************
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
Perhaps try: xcode-select –install
*********************************************************************************
error: command ‘cc’ failed with exit status 1

After installing libxml2, I still prompted this error, and finally found the problem after a lot of trouble

Need to add C_ INCLUDE_ Path to the libxml2 path in Xcode MacOS SDKs

sudo C_ INCLUDE_ PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2/

And then in execution

xcode-select --install
pip install lxml

How to Solve Django Error: No module named ‘MySQLdb’

Since many related dependent packages were unloaded when MySQL was unloaded, the following error occurred when Django was started after reloading MySQL:

django.core.exceptions.ImproperlyConfigured:Error loading MySQLdb module: No module named 'MySQLdb".
Did you install mysqlclient or MySQL-python?

Due to the development of Python version 3.6.4, mysql-python does not support python3, after a lot of trouble to install mysqlclient

The following is my process to solve the problem, I am lazy, I will show you in the form of pictures

First of all, I tried to install MySQL python, but there was an error. The following is the solution for online search:

Here’s how I found the solution and installed mysqlclient:

The following is the code to install mysqlclient and its dependent environment:

pip install mysqlclient
sudo apt-get install python3-dev libmysqlclient-dev

Conclusion:

When there is a problem with no module named ‘MySQL db’. It is recommended to install mysqlclient. MySQL Python does not support python3

[Solved] DjangoORM Run python manage.py makemigrations Error: no changes detected

no changes detected appears

python manage.py makemigrations
No changes detected

Why

When executing this command, he will go to all models and generate tables in the database

because sometimes when there are many app projects, he doesn’t know which model to look for and which model to generate the database table in

Here are models.Py

You need to add the name of the app project to the Django setting configuration file, such as CMDB

Then Django will go to the CMDB project to find models. Py to generate the database

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cmdb',
]

 

python manage.py makemigrations
Migrations for 'cmdb':
  cmdb\migrations\0001_initial.py
    - Create model UserInfo

The generation of this representative succeeded

[Solved] Microsoft Visual C++ 14.0 is required or warning: Debugger speedups using cython not found

Today, after downloading the installation of python3.64, we ran the crawler and reported an error:

warning: Debugger speedups using cython not found. Run '"C:\Python36\python.exe" "C:\Users\shaoks\.p2\pool\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\setup_cython.py" build_ext --inplace' to build.

pydev debugger: starting (pid: 10952)

I looked it up online and did the following:

First go to the C:\Python36\Scripts directory:

Run the following command:

python “C:\Program Files (x86)\JetBrains\PyCharm 2016.2.3\helpers\pydev\setup_cython.py” build_ext –inplace

Reported error:

error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio. com/visual-cpp-build-tools

Open your browser and type in: http://landinghub.visualstudio.com/visual-cpp-build-tools It says it’s expired

downloaded visualcppbuildtools full.exe into the installation, the installation process selects custom installation, the default options are as follows

After the installation is complete, continue with: python “C:\Program Files (x86)\JetBrains\PyCharm 2016.2.3\helpers\pydev\setup_cython.py” build_ext –inplace

Successful execution:

Then proceed to execute the crawler and crawl normally

ValueError: day is out of range for month [How to Solve]

Date out of range

At that time, I used the datetime module to generate time format data, and I mistakenly transmitted the result caused by wrong parameters. So, a good check of the data can solve the problem

As follows:

# Convert string type data to time structured data
# Originally, I wanted to write the following code
import datetime

date_init = '2019-05-10 00:00:00'

date_end = datetime.datetime(int(date_init.split('-')[0]), int(date_init.split('-')[1]),int(date_init.split('-')[2].split(' ')[0].lstrip('0')), int(date_init.split('-')[2].split(' ')[1].split(':')[0]), int(date_init.split('-')[2].split(' ')[1].split(':')[1]), int(date_init.split('-')[2].split(' ')[1].split(':')[2]))

print date_end,type(date_end)

#Hand shaking is wrong, as follows:

import datetime

date_init = '2019-05-10 00:00:00'

date_end = datetime.datetime(int(date_init.split('-')[0]), int(date_init.split('-')[1]),int(date_init.split('-')[2].split(' ')[0].lstrip('1')), int(date_init.split('-')[2].split(' ')[1].split(':')[0]), int(date_init.split('-')[2].split(' ')[1].split(':')[1]), int(date_init.split('-')[2].split(' ')[1].split(':')[2]))

print date_end,type(date_end)

Causes an error

In fact. Lstrip (‘0 ‘) can not be added. At that time, it was wrong to add parameters to datetime. Datetime (2019,05,05,00,00)

It’s over