Tag Archives: python

Python requests Error: ConnectionError: (‘Connection aborted.‘, error(104, ‘Connection reset by peer‘))

Python requests error

ConnectionError: (‘Connection aborted.’, error(104, ‘Connection reset by peer’))

There are two possible causes

1. Requests are too frequent, resulting in requests being rejected

Solution:

Set a sleep time for each request, such as
time sleep(1)

2. The interface has authentication or anti crawling mechanism. It is recognized that it is accessed by Python terminal, and the access is rejected

Solution:

Set user agent in request header to bypass authentication

as

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',"Content-Type": "application/json"}

SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX

Cause analysis: Python \ has the meaning of escape, so the read path is wrong

import os
os.walk('C:\Users\user\Desktop\Test')

Solution:
1. Add r in front of the path to prevent character escape.

import os
os.walk(r'C:\Users\user\Desktop\Test')

2. Use a double diagonal bar to read and output a backslash

import os
os.walk(r'C:\\Users\\user\\Desktop\\Test')

[Solved] error in REfO setup command: use_2to3 is invalid.

Problem Description: When using pip command to install refo==0.13 version, it will show that it is installed as 0.12 version because 0.13 version only has .tar.gz file and no whl file, pip installation will select 0.12 version, while manually downloading 0.13 version .tar.gz file and installing offline, it will report an error, the following is the process of the problem.

Requirement: install refo==0.13 version

pip install refo
#It will automatically install 0.12, because 0.13 does not have xhl files

pip install refo==0.13
#Error in REfO setup command: use_2to3 is invalid.
error in REfO setup command: use_2to3 is invalid.

# Download the 0.13 .tar.gz file and install it offline with the same error.
error in REfO setup command: use_2to3 is invalid.

Check the data and the problem is:

setuptools>= 58 interrupt support use_2to3

Setuptools change log for V58

setuptools should be updated to setuptools<Version 58 or avoid using use_2to3 set the package in the parameters.

Solution: Execute the following command:

pip install setuptools==56.0.0
#After execution, setuptools will automatically fall back to version 56.0.0

Then just install the refo==0.13 version.tar.gz file offline

Supplement the contents of the third-party package for offline installation:

Python third-party library download website Python third-party library

.whl file installation.

Go to the directory of the above whl file from the command line interface in one of the environments under anaconda where the library needs to be installed
Install the library using the command: pip install xxxxx.whl
.tar.gz file installation.

From the command line interface, go to the above extracted directory in the environment where the library is to be installed under anaconda
Use the command: python setup.py install

[Solved] ansible Command Error: Error -5 while decompressing data: incomplete or truncated stream

1. Error message:

An error occurs when executing the ansible command

ERROR! Unexpected Exception, this is probably a bug: Error -5 while decompressing data: incomplete or truncated stream

The detailed error reports are as follows

[root@localhost tmp]# ansible -h
ERROR! Unexpected Exception, this is probably a bug: Error -5 while decompressing data: incomplete or truncated stream
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible", line 97, in <module>
    mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass)
  File "/usr/lib/python2.7/site-packages/ansible/cli/__init__.py", line 38, in <module>
    from ansible.inventory.manager import InventoryManager
  File "/usr/lib/python2.7/site-packages/ansible/inventory/manager.py", line 36, in <module>
    from ansible.plugins.loader import inventory_loader
  File "/usr/lib/python2.7/site-packages/ansible/plugins/loader.py", line 22, in <module>
    from ansible.parsing.utils.yaml import from_yaml
  File "/usr/lib/python2.7/site-packages/ansible/parsing/utils/yaml.py", line 17, in <module>
    from ansible.parsing.yaml.loader import AnsibleLoader
  File "/usr/lib/python2.7/site-packages/ansible/parsing/yaml/loader.py", line 30, in <module>
    from ansible.parsing.yaml.constructor import AnsibleConstructor
  File "/usr/lib/python2.7/site-packages/ansible/parsing/yaml/constructor.py", line 29, in <module>
    from ansible.parsing.vault import VaultLib
  File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 42, in <module>
    from cryptography.hazmat.backends import default_backend
  File "/usr/lib64/python2.7/site-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
    import pkg_resources
  File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 70, in <module>
  File "build/bdist.linux-x86_64/egg/pkg_resources/extern/__init__.py", line 43, in load_module
    __import__(extant)
error: Error -5 while decompressing data: incomplete or truncated stream

2. Background of error reporting

When installing setuptools inansible-playbook, it terminated unexpectedly, and then execute ansible command to report this error,

 

3. Solution:

Reset setuptools-33.1.1.zip package decompression and installation:

cd setuptools-33.1.1  

python setup.py install

It is normal to try the ansible command after execution. Some friends may not be setuptools, or they may not execute after installing into a package. Please try it yourself.

[Solved] RuntimeError: unexpected EOF, expected 73963 more bytes. The file might be corrupted.

RuntimeError: unexpected EOF, expected 73963 more bytes. The file might be corrupted.

Problem Description:

When the project executes Python script, when downloading the pre training model weight of pytorch, if the weight is not downloaded due to network instability and other reasons, an error will be reported runtimeerror: unexpected EOF, expected xxxxx more bytes The file might be corrupted.


Cause analysis:

This error indicates that the downloaded weight file may be damaged. You need to delete the damaged weight file and execute the code to download again.


Solution:

To find the location where the downloaded weight file is saved, this paper analyzes three situations:

1. Windows System & Anaconda Environment

The path of download is D:\Anaconda3\anaconda\envs\yypy36\Lib\site-packages\facexlib\weightsdetection_Resnet50_Final.pt, so you need go to this folder and delete the weight file as the screenshot below:
2. Windows system & Python environment:

The code automatically downloads the model weights file and saves it to the C:\Users\username/.cache\torch\checkpoints folder. Note that .cache may be a hidden file, you need to view the hidden file to see it, just delete the weight file.

3. Linux systems:
Linux system weights files are usually saved under: \home\username\.cache\torch. Note that .cache is a hidden folder and will only be shown if you press ctrl+Alt+H in winSCP; or, in the home directory, use ls -a to show it. root mode, the default path for downloaded weight files is under: /root/.cache/torch/checkpoints. Just delete the weight file.

In the above three cases, after deleting the weight file, execute the code again to download again.

Additional:

If the execution program downloads the code too slowly or the network stability is not good, we can directly download it manually from the website where the weight file is located and put it in the specified location. The Linux system can adopt WGet method.

wget -P Local path where the weights are saved Address of the weights

If the download is interrupted, WGet supports continuous transmission at breakpoints. Add a parameter - C :

wget -P Local path where weights are saved -c Address of weights

eg:

wget -P /home/20220222Proj/pretrained_models -c https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth

import statsmodels.api as sm Error: ImportError: DLL load failed while importing _arpack: The specified program could not be found.

 

import statsmodels. API as SM reports the following errors:

The solution process is tortuous. Roughly, the SciPy version is incompatible with some methods of statsmodels. After scipy==1.6.0, the problem is solved:


(base) C:\Users\Administrator>pip uninstall statsmodels
Found existing installation: statsmodels 0.11.1
Uninstalling statsmodels-0.11.1:
  Would remove:
    d:\python\anaconda3\lib\site-packages\statsmodels-0.11.1.dist-info\*
    d:\python\anaconda3\lib\site-packages\statsmodels\*
Proceed (y/n)?y
  Successfully uninstalled statsmodels-0.11.1

(base) C:\Users\Administrator>pip install statsmodels==0.12.0 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting statsmodels==0.12.0
  Downloading http://pypi.doubanio.com/packages/37/22/959fdc710b6b384ad21a876130000557163978880dc50e16ba5d4ce35c18/statsmodels-0.12.0-cp38-none-win_amd64.whl (9.2 MB)
     |████████████████████████████████| 9.2 MB 6.4 MB/s
Requirement already satisfied: scipy>=1.1 in d:\python\anaconda3\lib\site-packages (from statsmodels==0.12.0) (1.5.0)
Requirement already satisfied: numpy>=1.15 in d:\python\anaconda3\lib\site-packages (from statsmodels==0.12.0) (1.20.3)
Requirement already satisfied: pandas>=0.21 in d:\python\anaconda3\lib\site-packages (from statsmodels==0.12.0) (1.3.4)
Requirement already satisfied: patsy>=0.5 in d:\python\anaconda3\lib\site-packages (from statsmodels==0.12.0) (0.5.1)
Requirement already satisfied: python-dateutil>=2.7.3 in d:\python\anaconda3\lib\site-packages (from pandas>=0.21->statsmodels==0.12.0) (2.8.1)
Requirement already satisfied: pytz>=2017.3 in d:\python\anaconda3\lib\site-packages (from pandas>=0.21->statsmodels==0.12.0) (2020.1)
Requirement already satisfied: six in d:\python\anaconda3\lib\site-packages (from patsy>=0.5->statsmodels==0.12.0) (1.15.0)
Installing collected packages: statsmodels
Successfully installed statsmodels-0.12.0

(base) C:\Users\Administrator>pip show scipy
Name: scipy
Version: 1.5.0
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author: None
Author-email: None
License: BSD
Location: d:\python\anaconda3\lib\site-packages
Requires: numpy
Required-by: -tatsmodels, xgboost, wxgl, sweetviz, stldecompose, statsmodels, sesd, seaborn, scikit-learn, scikit-image, resampy, pyod, mplsoccer, missingno, lightgbm, librosa, imgaug, imbalanced-learn, gensim, filterpy, factor-analyzer, combo, category-encoders, catboost, bayesian-optimization, albumentations

(base) C:\Users\Administrator>pip uninstall scipy
Found existing installation: scipy 1.5.0
Uninstalling scipy-1.5.0:
  Would remove:
    d:\python\anaconda3\lib\site-packages\scipy-1.5.0.dist-info\*
    d:\python\anaconda3\lib\site-packages\scipy\*
Proceed (y/n)?y
  Successfully uninstalled scipy-1.5.0
ERROR: Exception:
Traceback (most recent call last):
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\cli\base_command.py", line 188, in _main
    status = self.run(options, args)
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\commands\uninstall.py", line 89, in run
    uninstall_pathset.commit()
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\req\req_uninstall.py", line 450, in commit
    self._moved_paths.commit()
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\req\req_uninstall.py", line 290, in commit
    save_dir.cleanup()
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\utils\temp_dir.py", line 196, in cleanup
    rmtree(self._path)
  File "D:\python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 49, in wrapped_f
    return Retrying(*dargs, **dkw).call(f, *args, **kw)
  File "D:\python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 212, in call
    raise attempt.get()
  File "D:\python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 247, in get
    six.reraise(self.value[0], self.value[1], self.value[2])
  File "D:\python\Anaconda3\lib\site-packages\pip\_vendor\six.py", line 703, in reraise
    raise value
  File "D:\python\Anaconda3\lib\site-packages\pip\_vendor\retrying.py", line 200, in call
    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
  File "D:\python\Anaconda3\lib\site-packages\pip\_internal\utils\misc.py", line 135, in rmtree
    shutil.rmtree(dir, ignore_errors=ignore_errors,
  File "D:\python\Anaconda3\lib\shutil.py", line 737, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "D:\python\Anaconda3\lib\shutil.py", line 610, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "D:\python\Anaconda3\lib\shutil.py", line 610, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "D:\python\Anaconda3\lib\shutil.py", line 615, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "D:\python\Anaconda3\lib\shutil.py", line 613, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Denied to Access: 'D:\\python\\Anaconda3\\Lib\\site-packages\\~cipy\\fft\\_pocketfft\\pypocketfft.cp38-win_amd64.pyd'

(base) C:\Users\Administrator>pip install scipy --user -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting scipy
  Downloading http://pypi.doubanio.com/packages/56/a3/591dbf477c35f173279afa7b9ba8e13d9c7c3d001e09aebbf6100aae33a8/scipy-1.8.0-cp38-cp38-win_amd64.whl (36.9 MB)
     |████████████████████████████████| 36.9 MB 3.3 MB/s
Requirement already satisfied: numpy<1.25.0,>=1.17.3 in d:\python\anaconda3\lib\site-packages (from scipy) (1.20.3)
Installing collected packages: scipy
Successfully installed scipy-1.8.0

(base) C:\Users\Administrator>python
Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsmodels.api as sm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\python\Anaconda3\lib\site-packages\statsmodels\api.py", line 27, in <module>
    from .tsa import api as tsa
  File "D:\python\Anaconda3\lib\site-packages\statsmodels\tsa\api.py", line 31, in <module>
    from .filters import api as filters
  File "D:\python\Anaconda3\lib\site-packages\statsmodels\tsa\filters\api.py", line 6, in <module>
    from .filtertools import miso_lfilter, convolution_filter, recursive_filter
  File "D:\python\Anaconda3\lib\site-packages\statsmodels\tsa\filters\filtertools.py", line 18, in <module>
    from scipy.signal.signaltools import _centered as trim_centered
ImportError: cannot import name '_centered' from 'scipy.signal.signaltools' (C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\scipy\signal\signaltools.py)
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit()

(base) C:\Users\Administrator>pip install scipy==1.6 --user -i http://pypi.douban.com/simple --trusted-host pypi.douban.co
Looking in indexes: http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement scipy==1.6 (from versions: none)
ERROR: No matching distribution found for scipy==1.6

(base) C:\Users\Administrator>pip install scipy==1.6.0 --user -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting scipy==1.6.0
  Downloading http://pypi.doubanio.com/packages/a1/05/9b41f6a969a0d6ad36732bdd3c936434228dafaa829932be5a51574e4958/scipy-1.6.0-cp38-cp38-win_amd64.whl (32.7 MB)
     |████████████████████████████████| 32.7 MB 3.2 MB/s
Requirement already satisfied: numpy>=1.16.5 in d:\python\anaconda3\lib\site-packages (from scipy==1.6.0) (1.20.3)
Installing collected packages: scipy
  Attempting uninstall: scipy
    Found existing installation: scipy 1.8.0
    Uninstalling scipy-1.8.0:
      Successfully uninstalled scipy-1.8.0
Successfully installed scipy-1.6.0

(base) C:\Users\Administrator>python
Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsmodels.api as sm
>>>

After the problem is solved, restart the jupyter notebook and enter it for normal use.


[Solved] appium Error: Original error: socket hang up

App automation error reporting and appium error reporting:

Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command.original error: Could not proxy command to the remote server.original error: socket hang up

Solution:
uninstall appium setting and other appium installed apps on the device, uninstall the automatically started app (operation object APK), and restart the device

v2.error: OpenCV(4.5.5) error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours‘

Error Messages: cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2502: error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours’
Reason: Different Opencv versions cause errors

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)

Correction method: change “1” to “0”

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)

jupyter notebook Use pyLDAvis Error: AttributeError: module ‘pyLDAvis‘ has no attribute ‘gensim

Preparation
First, make sure you have the pyldavis and gensim libraries installed.

pip install gensim
pip install pyldavis

Code change
Secondly, if the error not attribute is because the version of pyldavis is different, the new version needs to be changed as follows: change all pyLDAvis.gensim to gensimvis。

import pyLDAvis.gensim as gensimvis
pyLDAvis.enable_notebook()
vis_sub_10 = gensimvis.prepare(lda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = gensimvis.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)

Success! The effect is shown in the following figure.

Or you can install a lower version of pyldavis

pip install pyLDAvis==2.1.2

Codes:

import pyLDAvis.gensim
pyLDAvis.enable_notebook()
vis_sub_10 = pyLDAvis.gensimda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = pyLDAvis.gensim.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)

[Solved] command “python setup.py egg_info“ failed with error code 1

 

preface

when setting up the python 3 environment on your own server, the PIP version is too low, the upgrade pip is still invalid and falls into a dead circle. After reading many blogs on the Internet, there is no solution


1. Solution

# Download get-pip.py
wget https://bootstrap.pypa.io/2.7/get-pip.py
python get-pip.py