Category Archives: Python

[Solved] Python Run Selenium Error: NameError: name ‘By’ is not defined

NameError: name 'By' is not defined

Reason: the by class does not have a package, and the system cannot recognize the by object

Solution:

Import by package:

from selenium.webdriver.common.by import By

———————————-Warm tips—————————————

Automatic package guide shortcut: Alt + ENTER

Pyinstaller Package Error: ImportError: OpenCV loader: missing configuration file: [‘config.py‘]

When writing Python today, I referenced CV2 when packaging with pyinstaller, and this error occurred:

ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

 

Create a new Python file and get the path of OpenCV:

import cv2
print(cv2.__file__) 

Here I get: C:\Anaconda3\lib\site-packages\cv2\__init__.py

After you reuse pyinstaller for packaging, add the paths parameter:

pyinstaller main.py -F --paths="C:\Anaconda3\lib\site-packages\cv2"

No error reported, successful operation!

[Solved] Pandas Parses MovieLens 1M Dataset Error: UnicodeDecodeError

1. Problem description

When learning from books, I find that the data set downloaded from GitHub will report an error when reading with pandas:

2. Solutions

It is obviously a coding problem. Use the file command to view the file code:

ISO-8859 the code in Python is iso-8859-1, which can be judged by the following function:

pip install chardet

def get_encoding(file):
    with open(file, 'rb') as f:
        return chardet.detect(f.read())['encoding']

Therefore, use the encoding parameter to specify the actual file format.

it’s fine too

movies = pd.read_table('movies.dat', encoding=get_encoding('movies.dat'), sep='::', header=None, names=mnames, engine='python')

[Solved] Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-*

1. Overview

When I use PIP3 install paramiko, an error is reported

...
        raise DistutilsError("Setup script exited with %s" % (v.args[0],))
    distutils.errors.DistutilsError: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-23ykqx51/pynacl/

Google has tried many methods of its predecessors for a long time, and finally found the answer.

pip3 install --upgrade pip

Then execute PIP3 install paramiko, but no error is reported, and the installation is successful.

[Solved] Jenkins Run python Project Error: “ModuleNotFoundError: No module named ‘xxx‘”

At runtime, jekins will take all folder paths of the current project as the search path of the package, while the command line only searches the current path by default.

Error message: modulenotfounderror: no module named ‘xxx’#

Solution:

Configure environment variables in jekins: system management → system settings (global settings and paths)

1.Global properties check Environment variables
2.Add new key-value pairs
Key:PYTHONPATH
Value:/var/lib/jenkins/workspace/project name

Django Use logging Module Cannot Operate the File Error: logging error Permission Error [WinError 32]

Causes:

This problem is only encountered during development, and the configuration is written to setting Py configuration file. We define the log file size. When the log is full, we will encounter this problem, because when running Django with pychart, we enable both processes to initialize setting Py configuration, one is accessed normally, and the other is used to monitor code changes (you will find that Django will rebuild itself every time you modify some code, which is completed by this process). Because it occupies the handle of the file, it fails during archiving

Solution:

Method 1: add the — noreload parameter when starting Django

For example, python manage py runserver 127.0.0.1:8080 –noreload

After adding the — noreload parameter, when the development server is started, the Python code of Django project is modified, and the server will not restart automatically

Method 2:

Expand log files

Modify setting.Py to expand maxbytes

[Solved] yarn Install Module Error:check python checking for Python executable “python2” in the PATH

Problem Description: when installing with yarn, node sass reports an error, as shown in the figure

Solution:

1. Uninstall node

2. Reinstall node and be sure to check the following steps

3. After node installation, the following script will pop up automatically. Click any key to continue

Note: Python, vs Build Tools and the installation tool chocolate for windows will be installed here.

4. The script here says that chocolate will be installed, and use this tool to install other tools. Click any to continue

5. Start PowerShell to install chocolate, Python and vs build tools

different versions of node have different vs and python installed. I installed Python 3 and vs2017 build tools. Due to the local environment, they have been installed, so the screenshot is as follows

6. the installation speed of vs build tools will be very slow. Do not forcibly stop powershells, otherwise the installation of vs build tools will be incomplete </ font>

7. After the installation is successful, chocolatey installs python2 choco install python2

8. Delete node_ Modules folder, yarn cache clean clear cache, reinstall, successful!

common problem

The installed node version is node-v14.0 18.2, so Python 3 and vs2017 build tools are installed

error msb4132: unrecognized tool version "2.0". The available tool versions are "14.0", "4.0"

Reason: a higher version of vsbuildtools is installed

Solution: use chocolate to install vs2017buildtools, and enter it on the command line

choco install visualstudio2017-workload-vctools --version 1.3.3

Chocolatey is slow to install python2. You can uninstall chocolatey and reinstall

it is useless to uninstall node, because chocoloatey will not be uninstalled automatically when node is uninstalled. Find the specific method by yourself.

View all installed software in the current system, and enter choco list - Li in the command line

[Solved] python MATLAB Error: OSError: MATLAB Engine for Python supports Python version 2.7, 3.4, 3.5 and3.6, but your version of python is 3.8

Because the coordinates of broken line graph and curve graph are to be read recently, Python is used to call matlab to try, but an error is reported in this process:

The matlab2017b I use does not support Python 3 Version 8.

Solution:

I chose to create a virtual environment in anaconda.

As follows:

Because the maximum version of 3.6 is supported, a version of 3.6 is created

Open pycharm, set it in setting, and select Add

Generally, the virtual environment created is in the envs of anaconda

After setting the environment, it can be executed successfully again

Test:

Run successfully

[Solved] django DRF This field may not be null, This field cannot be blank

target

You need to change the original mandatory field to non mandatory

realization

First, update the fields in models, and add null = true, default = none, blank = true

1
2
name = CharField(max_length=64, verbose_name='name', null=True, default=None, blank=True)
account = CharField(max_length=64, verbose_name='account', null=True, default=None, blank=True)

serializers update

1
2
name = CharField(required=False)
account = CharField(required=False)

execute python manage.py makemigrations
execute python manage.py migrate

Error:

May appear this error:

1
This field may not be null.

or

1
This field cannot be blank.

Solution:

You need to change serializers to this

1
2
name = CharField(required=False, allow_blank=True, allow_null=True)
account = CharField(required=False, allow_blank=True, allow_null=True)

allow_blank=True to solve This field cannot be blank.
allow_null=True to solve This field may not be null.

[Solved] 64-bit Python calls 32-bit Oracle client error: Cx_Oracle.DatabaseError: DPI-1047

problem

The operating system is 64 bit Python and 32 bit Oracle client. For other reasons, the Oracle client cannot be changed to 64 bit, resulting in Cx_Oracle64 bit cannot be used, CX_Oracle32 bit cannot be installed.

reason

Under Windows environment, if 64 bit Python is installed, CX_Oracle is used, the 64 bit Oracle client is called by default. In this case, we can only install Cx_Oracle win_Amd 64-bit version.

Solution:

1. First install CX_Oracle matching the current Python version, my is Python 3.8. The installed version is: Cx_Oracle-8.3.0-cp38-cp38-win_amd64.whl

2. Download the relevant Oracle instantclient (instant client) and unzip it to a folder convenient for calling

Download address: https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html

The version I downloaded is: instantclient-basiclite-windows-x64-19.13.0.0.0dbru.Zip, you can try several times, there is always one for you

3. Unzip the instantclient and change the environment variable in the Python code to adjust the Oracle driver location.

My unzipped address is:

E:\software\Python\instantclient-basiclite-windows.x64-19.13.0.0.0dbru

The code adjustment method is

import os

os.environ[‘path’] =  r’E:\software\Python\instantclient-basiclite-windows.x64-19.13.0.0.0dbru\instantclient_19_13′

import cx_Oracle

Now CX_Oracle can call normally