Tag Archives: Python

Solve the problem of error reporting when installing Python Library in pycharm

Recently, I was playing wechat Turing robot, but I installed some libraries, reported errors, and searched the Internet for a long time. I summarized two methods and recorded them

 

method 1:

Install manually, go directly to the official website and download the python library you need locally

Put it in the python installation path, C: \ users \ blue silver heart \ appdata \ local \ programs \ Python \ Python 37 \ lib \ venv \ scripts

PIP install – U library name

 

 

method 2:

Install in pycharm, but because many libraries are foreign and the domestic connection is not very friendly, some library installations will fail or fail to connect directly due to unstable network, such as

 

 

 

 

Solution

PIP source for domestic, I personally think Tsinghua source is relatively stable

Tsinghua source

https://pypi.tuna.tsinghua.edu.cn/simple/

 

 

 

 

Install again, successful

 

 

last

In comparison, it’s easier to install Python libraries automatically in Python. Manual installation is too cumbersome. I don’t like it

 

[Solved] Python Error: a bytes-like object is required, not ‘str’

Core code:

def ipPools(numPage):
    headers = randomHeads()
    url = 'http://www.xicidaili.com/nn/'
    saveFsvFile = open('ips.csv', 'wb')
    writer = csv.writer(saveFsvFile)
    for num in range(1, numPage + 1):
        full_url = url + str(num)
        re = requests.get(full_url, headers=headers)
        soup = BeautifulSoup(re.text, 'lxml')
        res = soup.find(id="ip_list").find_all('tr')
        for item in res:
            try:
                temp = []
                tds = item.find_all('td')
                proxyIp = tds[1].text.encode("utf-8")
                proxyPort = tds[2].text.encode("utf-8")
                temp.append(proxyIp)
                temp.append(proxyPort)
                writer.writerow(temp)
                print('保存为excel成功!')
            except IndexError:
                pass
Points to note.
   Be sure to convert str to bytes :
   str.encode("utf-8")
   python36 file method to open
   open('ips.csv', 'wb') change wb to w I got an error right here.  If there is the same error can, as a reference it!

[Solved] Python Error: /usr/bin/python^M: bad interpreter: No such file or directory

The main reason is ^ M

This is caused by different system coding formats: the. Sh. Py file edited in Windows system may have invisible characters, so the above abnormal information will be reported when executing in Linux system. It is usually caused by the different identification of the end of windows line and Linux line

Solution:

1) Conversion in Windows:

Use some editors, such as UltraEdit or EDITPLUS, to encode and convert scripts first, and then put them into Linux for execution. The conversion method is as follows (UltraEdit): File — > Conversions–> DOS-> UNIX is fine

2) Direct replacement under Linux

Sed – I’s/^ m// g ‘file name

3) It can also be converted in Linux

First, make sure that the file has executable permissions

#sh> chmod a+x filename

Then change the file format

#sh> vi filename

Use the following command to view the file format

: set FF or: set fileformat

You can see the following information

Fileformat = DOS or fileformat = UNIX

Use the following command to modify the file format

: set FF = UNIX or: set fileformat = UNIX

: WQ (save and exit)

Finally, execute the file

#sh>./ filename

Dill: solve the problem of Python’s “attributeerror: can’t pickle local object” and unable to pickle lambda function

How did “people you might know” find you on social software>>>

dill: solve the problem of Python’s “attributeerror: can’t pickle local object” and unable to pickle lambda function

Reference article:

(1) Dill: solve the problem of Python’s “attributeerror: can’t pickle local object” and unable to pickle lambda function

(2) https://www.cnblogs.com/dylanchu/p/11275817.html

Let’s make a note.

Problem solved successfully in Python: attributeerror: ‘module’ object has no attribute ‘loadimage‘

How did “people you might know” find you on social software>>>

Problem description

The source code is as follows:

import cv
from opencv.cv import *
from opencv.highgui import *

img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
    img = cv.QueryFrame(cap)
    cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))

This error occurred while running the code:

AttributeError: 'module' object has no attribute 'LoadImage'

Solutions

The reason for the problem is that the object “module” has no attribute “loadimage”

Guess that Import CV and from OpenCV. CV import * conflict, so try to annotate f ROM OpenCV. CV import *

Problem solving

When I change the code to the following:

import cv
#from opencv.cv import *
#from opencv.highgui import *

img = cv.LoadImage("test.jpg")
cap = cv.CreateCameraCapture(0)
while cv.WaitKey(1) != 10:
    img = cv.QueryFrame(cap)
    cv.ShowImage("cam view", img)
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))

Now the first mistake has been solved, and another one has appeared

AttributeError: 'module' object has no attribute 'LoadHaarClassifierCascade'

Through searching for information, we know that:

to load Haar classifier in opencv (in Python interface anyway), just use cv. Load

As follows:

import cv
cascade = cv.Load('haarcascade_frontalface_alt.xml')

The problem has been solved successfully

The bilibilibili cache video cache batch converted to MP4 format Python code

It is recommended to use virtual machine or Android mobile phone to download the cache

After the conversion, play more free, I hope to help you

Default cache location: Android – Data – ta.danmaku.bili – Download

How to use: put the script in the same directory as the cache, input the file name after execution, and then convert instantly

Only tested Mac, win should be universal, no test

Before conversion – after conversion:

import os, shutil #Import the required more reports
work_dir = os.getcwd() #Get the working directory


def main():
    old_dir = input("Enter the name of the directory to process") # Prompt for a filename similar to : 34387218
    new_dir = old_dir+"_MP4" # Enter the name of the file to be stored
    if(os.path.exists(new_dir)): # determine if it exists, if so delete and rebuild
        print(new_dir+"already exists, already rebuilt")
        shutil.rmtree(new_dir) 
        os.mkdir(new_dir)
    else:
        print(new_dir+"Created successfully")
        os.mkdir(new_dir)

    for i in os.listdir(os.path.join(work_dir, old_dir)): # Loop through
        if(i == ".DS_Store"): # Skip the file created by Apple by default
            continue
        for o in os.listdir(os.path.join(work_dir, old_dir, i)): # Start loop inside
            if(o == ".DS_Store"): # Skip files created by Apple by default
                continue
            if(os.path.isdir(os.path.join(work_dir, old_dir, i, o))): # Enter second level loop
                for p in os.listdir(os.path.join(work_dir, old_dir, i, o)): # Start the operation loop
                    if(o == ".DS_Store"): # Skip the file created by Apple by default
                        continue
                    file_name = os.path.basename(p) # Get the name of the file
                    if file_name.endswith(".blv"): # Determine if the name format is correct
                        f_file_name = file_name.split('.') # Split the file name
                        index = int(f_file_name[0]) + 1
                        old_file = os.path.join(work_dir, old_dir, i, o, p) # Get the old file
                        shutil.move(old_file,new_dir) # move the file
                        new_file = str(i)+"_"+str(index)+".mp4" # customize the new file name
                        os.rename(os.path.join(work_dir, new_dir,file_name), os.path.join(work_dir, new_dir, new_file)) # Perform rename
                    
                        
if __name__ == "__main__":
    main()

Python Standard Library: functools (cmp_to_key, lru_cache, total_ordering, partial, partialmethod, reduce, sing…

The objects handled by the functools module are all other functions, and any callable object can be regarded as a function for this module

1. functools.cmp_ to_ Key (func)
because Python 3 does not support comparison function, CMP_ to_ Key is to convert the old-fashioned comparison function into key function and use it together with functions that can accept key function, such as sorted, list. Sort, min, Max, heapq. Nlargest, itertools. Groupby, etc

examples:

from functools import cmp_to_key
    def compare(x1, x2):
        return x1 - x2
    
    a = [2, 3, 1]
    print(sorted(a, key=cmp_to_key(compare)))
    a.sort(key=cmp_to_key(compare))
    print(a)

Output:

[1, 2, 3]
[1, 2, 3]

2. @functools.lru_ cache(maxsize=128, typed=False)
lru_ Cache can be used as a decorator to cache the time-consuming results of function calculation to save time
because the dictionary is used for caching, the keyword parameters and location parameters of the function must be hashable
if maxsize = none, LRU function is disabled, and the cache can grow unlimited
when maxsize is the power of 2, LRU has the best performance

If typed is set to true, different types of function parameters, such as f (3) and f (3.0), will be cached separately, and will be regarded as different calls with different results

cache_ The info() function can be used to measure the validity of the cache and optimize the maxsize parameter. This function returns a named tuple of hits, misses, maxsize, and currsize
examples:

from functools import lru_cache


    @lru_cache(maxsize=32)
    def get_pep(num):
        resource = "http://www.python.org/dev/peps/pep-%04d/" % num
        try:
            with urllib.request.urlopen(resource) as s:
                return s.read()
        except urllib.error.HTTPError:
            return "Not Found"
    
    
    for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
        pep = get_pep(n)
        print(n, len(pep))
    
    print(get_pep.cache_info())
    
    
    @lru_cache(maxsize=None)
    def fib(n):
        if n < 2:
            return n
        return fib(n-1) + fib(n - 2)
    
    print([fib(n) for n in range(16)])
    print(fib.cache_info())

3. @functools.total_ Ordering
specifies a class that has one or more comparison sorting methods defined, and the class modifier provides the rest of the methods
this class must have been defined__ lt__(), __ le__(), __ gt__(), __ ge__() And defines__ eq__()

from functools import total_ordering


    @total_ordering
    class Student:
    
        def __init__(self, first_name, last_name):
            self.first_name = first_name
            self.last_name = last_name
    
        @staticmethod
        def _is_valid_operand(other):
            return hasattr(other, "last_name") and hasattr(other, "first_name")
    
        def __eq__(self, other):
            if not self._is_valid_operand(other):
                return NotImplemented
            return ((self.last_name.lower(), self.first_name.lower()) ==
                    (other.last_name.lower(), other.first_name.lower()))
    
        def __lt__(self, other):
            if not self._is_valid_operand(other):
                return NotImplemented
            return ((self.last_name.lower(), self.first_name.lower()) <
                    (other.last_name.lower(), other.first_name.lower()))
    
    
    a = Student(first_name="tom", last_name="hancs")
    b = Student(first_name="tom", last_name="hancs")
    print(a == b)
    print(a <= b)
    print(a >= b)

If you don’t use total_ To decorate a decorator, use & lt= Or & gt= Error will be reported:

Traceback (most recent call last):
  File "D:/LearnProject/performance/functools_test.py", line 33, in <module>
    print(a <= b)
TypeError: '<=' not supported between instances of 'Student' and 'Student'

4. Functools. Partial (func, * args, * * keywords)
partial() is used to freeze function parameters or one of the key parts, and generate a new function object that simplifies the parameter passing in<
example:

from functools import partial

basetwo = partial(int, base=2)
print(basetwo('111'))

5. Functools. Partialmethod (func, * args, * * keywords)
function is similar to partial, usage is as follows:

from functools import partialmethod


    class Cell(object):
    
        def __init__(self):
            self._alive = False
    
        @property
        def alive(self):
            return self._alive
    
        def set_state(self, state):
            self._alive = bool(state)
    
        set_alive = partialmethod(set_state, True)
        set_dead = partialmethod(set_state, False)
    
    
    c = Cell()
    print(c.alive)
    c.set_alive()
    print(c.alive)

6. Reduce (function, iterative [, initializer])
cumulate the functions of two parameters from left to right to sequence items, such as reduce (lambda x, Y: x + y, [1, 2, 3, 4, 5]), which is equivalent to calculating (((1 + 2) + 3) + 4) + 5)

from functools import reduce
    print(reduce(lambda x, y: x+y, range(0, 10)))

7. @ functools. Singledispatch
when a function needs to determine the output content according to the type of the input variable, the usual way is to use a large number of if/elif/else to solve the problem inside the function
this will make the code bulky, difficult to maintain, and not easy to expand

from functools import singledispatch


    @singledispatch
    def func(arg, verbose=False):
        if verbose:
            print("Let me just say,", end=" ")
        print(arg)
    
    
    @func.register(int)
    def _(arg, verbose=False):
        if verbose:
            print("Strength in numbers, eh?", end=" ")
        print(arg)
    
    
    @func.register(list)
    def _(arg, verbose=False):
        if verbose:
            print("Enumerate this:")
        for i, elem in enumerate(arg):
            print(i, elem)
    
    
    def nothing(arg, verbose=False):
        print("Nothing.")
    
    func.register(type(None), nothing)
    
    
    @func.register(float)
    def fun_num(arg, verbose=False):
        if verbose:
            print("Half of your number:", end=" ")
        print(arg/2)
        
    
    func("Hello, world.")
    func("test.", verbose=True)
    func(42, verbose=True)
    func(["spam", "spam", "eggs", "spam"], verbose=True)
    func(None)
    func(1.23)
    
    # Check which implementation the generic function will choose for a given type
    print(func.dispatch(float))
    print(func.dispatch(dict))
    
    # Access to all registered implementations
    print(func.registry.keys())
    print(func.registry[float])

8. functools.update_ wrapper(wrapper, wrapped, assigned=WRAPPER_ ASSIGNMENTS, updated=WRAPPER_ Updates)
used to update the__ name__,__ doc__ To make it look like a decorated function

from functools import update_wrapper
    
    
    def wrap(func):
        def call_it(*args, **kwargs):
            """call it"""
            return func(*args, **kwargs)
        return call_it
    
    
    @wrap
    def hello():
        """say hello"""
        print("hello world")
    
    
    def wrap2(func):
        def call_it2(*args, **kwargs):
            """call it2"""
            return func(*args, **kwargs)
        return update_wrapper(call_it2, func)
    
    
    @wrap2
    def hello2():
        """say hello2"""
        print("hello world2")
    
    
    print(hello.__name__)
    print(hello.__doc__)
    
    print(hello2.__name__)
    print(hello2.__doc__)

9. @functools.wraps(wrapped, assigned=WRAPPER_ ASSIGNMENTS, updated=WRAPPER_ Updates)
decorators that use wraps can retain the properties of the decorated function

from functools import wraps
    
    
    def my_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            print("Calling decorated function")
            return f(*args, **kwargs)
        return wrapper
    
    
    @my_decorator
    def example():
        """Example Docstring"""
        print("Called example function")
    
    
    example()
    print(example.__name__)
    print(example.__doc__)

The examples in this article come from Python official documents and the Internet

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

Pep8: continuation line over indented for visual indent appears when wrapping Python code

Website content quality is poor, distribution efficiency is too low how to do?Huawei engineers offer 5 unique skills>>>

In pycharm editor, when a line of code is too long and needs to wrap, use the symbol connection, such as:

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:

However, the editor will prompt: pep8: continuation line over indented for visual indent, you can use () to package the code in it, and you don’t need the symbol, such as:

if (first_index < 0 or
    second_index > self._number_of_plates - 1):

Python decision tree visualization: a solution to graphviz’s executables not found

Why are there a series of technical challenges behind “OMG buy it”>>>

Python decision tree visualization: graphviz’s executables not found solution

Reference article:

(1) Python decision tree visualization: graphviz’s executables not found solution

(2) https://www.cnblogs.com/nxf-rabbit75/p/11134508.html

Let’s make a note.