Category Archives: Python

[Solved] pyautogui.PyAutoGUIException: PyAutoGUI was unable to import pyscreeze.

“PyAutoGUI was unable to import pyscreeze. (This is likely because you’re running a version of Python that Pillow (which pyscreeze depends on) doesn’t support currently.) Please install this module to enable the function you tried to call.”
pyautogui.PyAutoGUIException: PyAutoGUI was unable to import pyscreeze. (This is likely because you’re running a version of Python that Pillow (which pyscreeze depends on) doesn’t support currently.) Please install this module to enable the function you tried to call.

solution:

pip install pyscreeze

Download the dependency package to solve the problem

2. Because I used the python plug-in of idea, I didn’t choose my own Python installation path when I first created the python project, so idea automatically created a python running environment for me on disk F. however, the python commands I used on the command line and the downloaded packages are on disk d.. Finally, the problem was solved by changing the running environment:

File ➡ project Structure ➡ SDKs

Select python.exe under your own Python directory

Then restart and reload the running environment

[Solved] Scrapy Error: ERROR: Error downloading & RequestGenerationFailed

Pit review

In recent days, I have realized crawling Baidu hot search movies with scratch, which can be successfully crawled on windows, but when I put the code on Ubuntu 18.04 virtual machine, I didn’t get the content, and the following error occurred:

Solution:

I compared the following version information of their components, that is, this part of the information.

I found that the cryptography version of the virtual machine was obviously low, so I tried to upgrade to the lower level:

pip3 install --upgrade cryptography 

Then run again and find that suddenly it’s OK!

Pytest executes the case.py file on the terminal error: Modulenotfounderror: no module named ‘API’

Use pytest to execute the case.py file on the terminal and report an error   Modulenotfounderror: no module named ‘API’. Actually, I have an API package,

The right-click run in pycharm can be executed.

In the first line of the case file to be executed (which must be the front of all references), add

import os,sys
sys.path.append(r"D:/python-workspace/interfacePytest") 

append followed by the absolute path to the project

Python Error: Non-UTF-8 code starting with ‘\xe7’ [How to Solve]

Phenomenon:

Copy a normal py file, modify part of the code, save and execute Python demo.py. An error message appears: syntax error: non-utf-8 code starting with ‘\ xe7’

reason:

The text encoding copied elsewhere may not be utf8

Solution 1:

Python 3 uses UTF-8 format by default

Generally, you don’t need to add it at the beginning  # -*- coding:utf-8 -*-

However, some Chinese characters are still unrecognized and throw non-utf-8 code starting with ‘\ xe7’   If you are wrong, you need to add this sentence to the first line.

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

Solution 2:

Python 3 demo.py so that there will be no error prompt during execution

Solution 3:

Click the encoding UTF-8 in the lower right corner of vscode, save the current file as UTF-8, and then execute Python demo.py without error prompt.

[Solved] Pytorch load pre-training model Error: modulenotfounderror: no module named ‘models’

Problem Description:

When using pytorch to load the pre training model, an error occurs: modulenotfounderror: no module named ‘models’. The pre training model is yolov5s.pt downloaded from the official

Solution:

Build the directory structure of yolov5 under the wrong file directory. That is, you need to exist yolov5 models and utils, and copy these two folders to the file directory. For example, at this time, the location where I report an error is yolov5/train.py. Because I use yolov5 for transplantation, the yolov5 directory is not a real yolov5, so I will report missing models. The deeper reason is that the following method is used to save weights (there is another way to save and load models, which can be viewed in the official document of pytorch. That method will not cause this problem and is more conducive to migration):

The weights saved in this way will also save the relative position of the source code of your model by default (here refers to the models folder and utils folder in yolov5).Yolo5s.pt downloaded from the official must be trained under this directory structure, so we can build this directory structure.

[Solved] Pyinstaller package Error: AttributeError: ‘str’ object has no attribute ‘decode’

(Ven) F:\project\Python\Freightsmart>pyinstaller -D sutep.py
83 INFO: PyInstaller: 4.7
83 INFO: Python: 3.7.4
83 INFO: Platform: Windows-10-10.0.19041-SP0
84 INFO: wrote F:\project\Python\Freightsmart\sutep.spec
90 INFO: UPX is not available.
114 INFO: Extending PYTHONPATH with paths
['F:\\project\\Python\\Freightsmart']
Traceback (most recent call last):
  File "E:\Python\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "E:\Python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\project\Python\Ven\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "f:\project\python\ven\lib\site-packages\PyInstaller\__main__.py", line 124, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "f:\project\python\ven\lib\site-packages\PyInstaller\__main__.py", line 58, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\building\build_main.py", line 782, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "f:\project\python\ven\lib\site-packages\PyInstaller\building\build_main.py", line 714, in build
    exec(code, spec_namespace)
  File "F:\project\Python\Freightsmart\sutep.spec", line 19, in <module>
    noarchive=False)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\building\build_main.py", line 245, in __init__
    self.hookspath += discover_hook_directories()
  File "f:\project\python\ven\lib\site-packages\PyInstaller\building\build_main.py", line 116, in discover_hook_directories
    """
  File "f:\project\python\ven\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 91, in exec_statement
    return __exec_statement(statement, capture_stdout=True)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 75, in __exec_statement
    return __exec_python_cmd(cmd, capture_stdout=capture_stdout)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 66, in __exec_python_cmd
    txt = compat.exec_python(*cmd, env=pp_env)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\compat.py", line 546, in exec_python
    return exec_command(*cmdargs, **kwargs)
  File "f:\project\python\ven\lib\site-packages\PyInstaller\compat.py", line 348, in exec_command
    out = out.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'

 File "f:\project\python\ven\lib\site-packages\PyInstaller\compat.py", line 348, in exec_command

Commont out
        # if encoding:
        #     out = out.decode(encoding)
        # else:

[Solved] Python Error: UnicodeDecodeError: ‘gb2312’ codec can’t decode byte 0xa4 in position… : illegal multibyte sequence

1. Error reporting scenario

An error is reported when using Python: “Unicode decodeerror: ‘GB2312’ codec can’t decode byte 0xa4 in position…: illegal multibyte sequence” generally has the following two scenarios:

1. Crawl Chinese website content

html = requests.get(url).decode("gb2312")

2. Read GBK encoded files

result = open(filename, 'r', encoding='gb2312')

2. Error reporting reason

The Chinese character set included in GB2312 is not comprehensive enough, and decoding errors will occur when encountering traditional characters.

Chinese character set range GB2312 < gbk < gb18030

3. Error reporting solution

1. Ignore decoding errors

html = requests.get(url).decode('gb2312',errors = 'ignore')

The default parameter of the decode function is strict. Decode ([encoding], [errors =’strict ‘]). You can use the second parameter to control the error handling strategy. Strict means that an exception is thrown when an illegal character is encountered
if it is set to ignore, illegal characters will be ignored
if it is set to replace, it will be used? Replace illegal characters

2. Replace gbk2312 with GBK with a more comprehensive Chinese character set

result = open(filename, 'r', encoding='gbk')

Note: if ‘ignore’ is used to ignore illegal characters and report errors, the read Chinese will be garbled. If you want to read accurate Chinese content, you can first convert the content encoded in GB2312 to UTF-8 and then read it.

res = requests.get(url)
res = decode(res, "gb2312").encode("utf8")
res.encoding = 'utf-8'
html = res.text
print(html)

At this time, Chinese characters can be output normally.

[Solved] UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x89 in position 0: invalid start byte

Reproduced in: error Unicode decodeerror during program operation: ‘UTF-8’ codec can’t decode byte 0x89 in position 0: invalid start byte

According to the solutions of others on the Internet, it is judged that there is a problem with the code entered in the file

For example:

It is solved by adding encoding = “unicode_escape” in the line of code reading the file, but it is useless to my program.

My one line code with file reading is

fh = open('data/example.png', 'r')

Find the previous Python notes. I think the syntax of reading the file is wrong. Change this line to

fh = open('data/example.png', 'rb')

[Solved] HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.

Original method of error reporting:

1) Using request.request, the above error occurs .HTML cannot be crawled

from urllib import request

def get_html(self, url):
    print(url)
    req = request.Request(url=url, headers={'User-Agent': random.choice(ua_list)})
    res = request.urlopen(req)
    # html = res.read().decode()
    html = req.read().decode("gbk", 'ignore')
    with open(filename, 'w') as f:
        f.write(html)
    self.parse_html(html)

Solution:

1) Replace urllib.request with the requests library, which needs to be reinstalled.

2) I don’t know the specific reason.

  import requests
    def get_html(self, url):
        print(url)
        req = requests.get(url=url, headers={'User-Agent': random.choice(ua_list)})
        req.encoding = 'utf-8'
        # print(req.text)
        # res = request.urlopen(req)
        # html = res.read().decode()
        # print(req)
        # html = req.read().decode("gbk", 'ignore')
        # print(html)
        # Call the analytic function directly
        # filename = '123456.html'
        # with open(filename, 'w') as f:
        #     f.write(html)
        self.parse_html(req.text)

Command “python setup.py egg_info” failed with error code 1

D:\Program Files\python_3_6_4>python -m pip install –upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
Cache entry deserialization failed, entry ignored
Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
100% |████████████████████████████████| 1.7MB 299kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.1
Uninstalling pip-9.0.1:
Successfully uninstalled pip-9.0.1
Successfully installed pip-21.3.1

Special Note.
If multiple versions of python are installed
you must execute python -m pip install –upgrade pip in the correct location (directory, folder)