Tag Archives: python

[Solved] Fatal Python error: initfsencoding: Unable to get the locale encoding

Error Message:

Fatal Python error: initfsencoding: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

 

Solution:

#1.unset
unset PYTHONHOME
unset PYTHONPATH
#2.update pip
python -m pip install --upgrade pip

Then it can be solved.

pip install decord failed. The reason is that pip has a problem. After the update, it is installed normally

[Solved] YOLOv5 Error: SyntaxError: EOL while scanning string literal

1. Error details

When running the detect.py file of YOLOv5 module, the following error occurs:

Complete error reporting code:

  File "C:\Projects\pythonProject\yolov5-5.0\detect.py", line 110
    with open(txt_path + '.txt', 'a') as f:
                                           ^
SyntaxError: EOL while scanning string literal

This problem occurs because detect.py encoding error

2.Solution

Delete the two lines of code at the top of the file, and the problem can be solved and detected.

# -*- coding : utf-8-*-
# coding:unicode_escape

[Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function ‘circle‘

Here, I operate on the image

import cv2
import numpy as np
img=cv2.imread('a.jpg',cv2.IMREAD_GRAYSCALE)
row,col=img.shape[:2]
print(row,col)
circle=np.zeros((row,col),dtype='uint8')
cv2.circle(circle,(row/2,col/2),100,255,-1)
result=cv2.bitwise_and(img,circle)
cv2.imshow('a',img)
cv2.imshow('circle',circle)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

An error is found during operation, as shown in the following figure

The error reported is a type error, which makes it impossible to analyze the coordinates of the center of the circle. This is mainly because our center coordinate is obtained by dividing the width and height of the picture by two. It is of float type, and the others are of int type. We can convert the center coordinate type to int type

cv2.circle(circle,(row//2,col//2),50,255,-1)

The results after operation are shown in the following figure:

 

[Solved] Django Configurate celery error: django.db.utils.DatabaseError

Error on execution of asynchronous task: 

django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias ‘default’ was created in thread id 3047101641304 and this is thread id 3047208084584.

 

Solution:

Original start command (under Windows):
celery -A xxx worker -l info -P eventlet (xxx is the project name)
Modified start command, celery version 4x:
celery -A xxx worker -l info -P solo

Python draw error: ValueError: ‘color’ kwarg must be a color or sequence of color specs. For a sequence of values to b

Error Message:

ValueError: ‘color’ kwarg must be a color or sequence of color specs. For a sequence of values to b
Error Codes:

plt.scatter(data0[:,0],data0[:,1],color='',edgecolor='green',marker='o')

Solution:

plt.scatter(data0[:,0],data0[:,1],color='white',edgecolor='green',marker='o')

How to Solve Pytorch eval Stuck Error

Question

The single card training is very fast. When it comes to eval, it doesn’t move after running a batch, and there is no error.

Tried, still not moving

1, change the pin_memory of valid_loader to False. if it is True, it will automatically load the data into pin_memory, which speeds up the data
transfer speed to GPU.
2, change num_workers to 1, some people say too many workers may lead to multi-process interlock, can reduce or not

 

Final Solution:

valid_loader:
pin_memory = true # this is very important. Before, people on the Internet said that changing false might solve the problem. My experiment proved that if you do not work, you can run normally by changing back to true.
num_workers=4
batch_size=8

train_loader:
pin_memory=True
num_workers=4
batch_size = 8
these parameters are the same as valid_loader

In general, first of all, the pin_memory of valid_loader is kept True, which is well understood, the data is automatically loaded into pin_memory, which speeds up the data transfer to the GPU and naturally speeds up the inference process. Then, the number of workers and batch_size is reduced, and both valid_loader and train_loader are reduced. pin_memory of train_loader is also kept True.

[Solved] LeNet Script Train Error: AttributeError: ‘DictIterator’ object has no attribute ‘get_next’

My training environment:

Windows10 64bit;

MindSpore1.5.0-beta;

CPU;

python3.9;

When training Mnist data set with LeNet , the following error occurs

How to solve this problem??

It’s true that the version is a bit old – the above use case needs to be modified if implemented on a newer version – from the current implementation of Iterator’s code, it no longer has the get_next method: https://gitee.com/mindspore/mindspore/blob/master/mindspore/python/mindspore/dataset/engine/iterators.py#L59

But it has __next__ method, so the above line can be modified, you can try it: > original: data = ds.get_next() > modified: data = next(ds)

How to Solve wikiextractor Extract Wikipedia Corpus Error

When I extracted Wikipedia corpus, I first used the wikiextractor. Later, I found that it was always wrong, so it was useless. Since many people asked me how to extract the corpus, I now publish the code

I didn’t write the code, but I found it from a website. Because it took too long and I forgot the address of the website, I can’t post the original URL. If the author sees it, please send a private letter to my original URL

The author’s email address is: [email protected]

How to use: enter the command at the command line:

python data_pre_process.py zhwiki-latest-pages-articles.xml.bz2(Wikipedia Corpus) wiki.zh.text(Saved files)

Source code:

# -*- coding: utf-8 -*-
# Author: Pan Yang ([email protected])
# Copyrigh 2017
from __future__ import print_function

import logging
import os.path
import six
import sys

from IPython.core.page import page
from gensim.corpora import WikiCorpus

page.encoding = 'utf-8'

# Wrapping the Wikipedia xml corpus into txt format
# python data_pre_process.py zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text
if __name__ == '__main__':
    program = os.path.basename(sys.argv[0])
    logger = logging.getLogger(program)

    logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
    logging.root.setLevel(level=logging.INFO)
    logger.info("running %s" % ' '.join(sys.argv))

    # check and process input arguments
    if len(sys.argv) != 3:
        print("Using: python process_wiki.py enwiki.xxx.xml.bz2 wiki.en.text")
        sys.exit(1)
    inp, outp = sys.argv[1:3]
    space = " "
    i = 0

    output = open(outp, 'w', encoding='utf-8')
    wiki = WikiCorpus(inp, dictionary={})
    for text in wiki.get_texts():
        if six.PY3:
            output.write(bytes(' '.join(text), 'utf-8').decode('utf-8') + '\n')
            #   ###another method
            #   output.write(space.join(map(lambda x: x.decode("utf-8"), str(text))) + '\n')
        else:
            output.write(space.join(text) + "\n")
        i = i + 1
        if i % 10000 == 0:
            logger.info("Saved " + str(i) + " articles")

    output.close()
    logger.info("Finished Saved " + str(i) + " articles")

ModuleNotFoundError: No module named ‘requests‘ [How to Solve]

Problem description: After installing the requests module using pip under cmd, I can use import requests, but I can’t import it under Pycharm IDE with the following error: ModuleNotFoundError: No module named ‘requests ‘

Later I found out that my python was installed on E drive, but I installed it on C drive with the install requests command, without switching E drive. I reinstalled it on the E drive

Installation steps

①cmd window: switch to the Scripts file in the python installation directory on disk E

②Install command: pip install requests, but there is a yellow text error

③ Follow the Yellow prompt command and enter: python -m pip install --upgrade pip to complete the installation

④ Prove that the installation is successful: you can enter the command under Python: import requests command does not report an error

Pycharm page

[Solved] Appium Error: InvalidArgumentException: Message: invalid argument: invalid locator

Appium Error:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

 

Solution: Add the following two parameters when you create a driver.

desired_caps[‘chromeOptions’] = {‘w3c’:False}
desired_caps[‘showChromedriverLog’] = True

 

Refer:
Fail to locate an web element using “findElement” on Android 9 using Appium 1.15.0 · Issue #13306 · appium/appium · GitHub

[Solved] Python Requests Error: simplejson.errors.JSONDecodeError: Expecting value

Problem: when running the interface automation script, the requested data is correct, but it keeps reporting error: simplejson.errors Jsondecodeerror: expecting value


reason:
the reason for the error reported by the author here is that this interface returns: response in the non-JSON format
while in the basic interface code, the return is res.json()

Solution:
Modify to return text, or add an exception capture

exception capture

Nltk Library Download error: [errno: 11004] getaddrinfo failed

Download the natural language processing nltk library and install it under win10. The error is as follows:

IP address of raw.githubusercontent.com could not found

since the hosts cannot be modified and saved in normal mode, it needs to be run in management mode

Solution:

  1. Run Power Shell as administrator
  2. Go to the hosts directory: C:\Windows\system32\drivers\etc
  3. Enter cmd, notepad hosts, and enter to modify the hosts file.
  4. Add the website ip address and URL in the last line of the hosts file and save it.


If the IP address of raw.github is changed frequently, you need to query the URL first: open the URL to query the IP address: https://www.ipaddress.com/ Enter raw.githubusercontent Com to get the IP address

Test succeeded