Category Archives: Python

[Solved] Error when Python reads a file Unicode decodeerror

python read files error: UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 205: illegal multibyte sequence

python read file warning: “UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 205: illegal multibyte sequence”

Solution 1:

FILE_OBJECT= open('data.txt','r', encoding='UTF-8')

Solution 2:

FILE_OBJECT= open('data.txt','rb')

 

[Solved] Python installation error: One or more issues caused the setup to fail.

After python 3.7.3 is installed, clicking on the exe starts an error

One or more issues caused the setup to fail. Please fix the issues and then retry setup. For more information see the log file.

Windows 7 Service Pack 1 and all applicable updates are required to install Python 3.6.4(64-bit).

Please update your machine and then restart the installation.

Reason.

Windows system problem, because the version is too old, need to need to install the SP1 package upgrade package

Click on the link above and select the red mark to download, the download will be completed and you are good to go.

 

Error reported by python console in Pycharm [How to Solve]

Error:Error:Cannot run program “E:\PythonProjects\venv\Scripts\python.exe” (in directory “E:\ProjectsHttpR”): CreateProcess error=2, system could not find the specified file.

Solution:

1 file–setting–Project: project name–Project Interpreter–path select the python installation path.

2 file–setting–Buile,Execution,Depolyment–Console–Python Console–Python interpretr. path Select the installation path of python.

 

Python import random error handling method [Solved]

python installation failed: error reported on make.

/usr/include/tkDecls.h:1542: error: expected ‘)’ before ‘*’ token
/usr/include/tkDecls.h:1639: error: expected declaration specifiers or ‘…’ before ‘Drawable’
/usr/include/tkDecls.h:1674: error: expected ‘)’ before ‘*’ token
/usr/include/tkDecls.h:1679: error: expected ‘)’ before ‘*’ token
/usr/include/tkDecls.h:1710: error: expected specifier-qualifier-list before ‘XColor’

Failed to find the necessary bits to build these modules:
bsddb185 dl imageop
sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

Failed to build these modules:
_tkinter binascii zlib

The above error, resulting in

[storm@bs035 rpm]$ python
Python 2.6.6 (r266:84292, Apr 12 2016, 18:51:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import random
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “/usr/local/lib/python2.6/random.py”, line 48, in <module>
from binascii import hexlify as _hexlify
ImportError: No module named binascii
>>>

Solution

The python version is not working properly causing this.

Upgrade installationzlib
# wget http://sourceforge.net/projects/libpng/files/zlib/1.2.8/zlib-1.2.8.tar.gz
# tar -zxvf zlib-1.2.8.tar.gz
# cd zlib-1.2.8
# ./configure
# make install

upgradesqlite3
# cd ..
# wget http://www.sqlite.org/2014/sqlite-autoconf-3080500.tar.gz
# tar -zxvf sqlite-autoconf-3080500.tar.gz
# cd sqlite-autoconf-3080500
# ./configure
# make
# make install

# cd ../Python-2.7.3
# make –s
# make && sudo make install

 

Django logs detailed error reporting information

When the server 500 makes an error, the ordinary log will only record the request information of 500 in one line, and will not record the detailed error location

[ERROR] 2019-06-12 15:07:03,597 "GET /api/v1/test/ HTTP/1.1" 500 74196

You need to add a middleware that records detailed error information in the log

# -*- coding: UTF-8 -*-
import logging

logger = logging.getLogger('default')


class ExceptionLoggingMiddleware(object):
    def process_exception(self, request, exception):
        import traceback
        logger.error(traceback.format_exc())

Add this middleware to the midview of settings_In classes

MIDDLEWARE_CLASSES = (
'utils.my_middleware.ExceptionLoggingMiddleware',
)

Use Django logger

    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'INFO'
        },

Effect

[ERROR] 2019-06-12 15:07:02,265 Internal Server Error: /api/v1/test/
Traceback (most recent call last):
  File "E:\tcp_check\venv\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "E:\tcp_check\venv\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\tcp_check\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\tcp_check\tcp_test_app\views.py", line 23, in test
    a=1/0
ZeroDivisionError: division by zero
[ERROR] 2019-06-12 15:07:03,597 "GET /api/v1/test/ HTTP/1.1" 500 74196

 

python3.7 pip install livetest Error [How to Solve]

pip install livetest error screenshot:

Problem Cause: There is a compatibility issue.

Problem solved.

1. Download https://files.pythonhosted.org/packages/de/77/7ab378ff9e62013f1756b64b1553e126ba7b3f3598bc9c5c8fc508c13d55/livetest-0.5.tar.gz

2. Modify \livetest-0.5\livetest\__init__.py

Modify import httplib -> import http.client as httplib
modify import urlparse -> import urllib.parse
modify from Cookie import BaseCookie, CookieError -> import http.cookiejar

Modify XXXError, e to XXXError as e

3. Modify \livetest-0.5\setup.py

modify long_description=file(‘README.rst’).read() -> long_description=open(‘README.rst’).read()
modify version=livetest.__version__-> version=’0.5′
Modify author_email=livetest.__author__-> author_email=’[email protected]

4. Switch the path to \livetest-0.5 in cmd, execute python setup.py install and install successfully.

 

Python Ssl.certificateerror errors [How to Solve]

Some websites do not obtain the security certificate issued by the browser. When you request this website, the browser will treat it as an unsafe website, so it will report ssl.certificate error

The solution is to modify the default certificate verification mode to no verification code, as follows:

from urllib import request
#Importing the authentication module
import ssl
# assign the default certificate validation mode to no validation
ssl._create_default_https_context = ssl._create_unverified_context
base_url = 'https://www.12306.cn/mormhweb/'
# initiate request
response = request.urlopen(base_url)
 
print(response.read().decode('utf-8'))

Finally, you will find that the error is not reported and the problem is solved