Tag Archives: python

Python Connect database error: command listdatabases requires authentication

Python reports an error when connecting to the database. Command listdatabases requires authentication, full error: {OK ‘: 0.0,’ errmsg ‘:’ command listdatabases requires authentication ‘,’ code ‘: 13,’ codename ‘:’ unauthorized ‘}

The reason for the error is that authentication is required, indicating that user name and password authentication are required to connect to mongodb database.

Connect mongodb without password. The code is as follows:

from pymongo import MongoClient
class MongoDBConn:
    def __init__(self, host, port, db_name, user, password):
        """
        Establishing database connections
        """
        self.conn = MongoClient(host, port)
        self.mydb = self.conn[db_name]

With password authentication, connect to Mongo database, and the code is as follows:

from pymongo import MongoClient

class MongoDBConn:

    def __init__(self, host, port, db_name, user, password):
        """
        Establishing database connections
        """
        self.conn = MongoClient(host, port)
        self.db = self.conn.admin 
        self.db.authenticate(user, password)
        self.mydb = self.conn[db_name]

Pit record

In fact, an error was reported in the middle:

Authentication failed., full error: {‘ok’: 0.0, ‘errmsg’: ‘Authentication failed.’, ‘code’: 18, ‘codeName’: ‘AuthenticationFailed’}

Originally used directly in the code:

self.db = self.conn[db_name]
self.db.authenticate(user, password)

If you directly use the target database for link authentication, you will report the above error. Instead, first connect to the system default database admin, use admin for authentication, and you will succeed, and then do the corresponding operation for the target database.

pytorch model.load_state_dict Error [How to Solve]

When pytorch loads the model, if some judgment is used in the model, the judgment is used as the selection execution condition, but it is also saved in the model. However, when calling, the network in the judgment condition is not selected and load_state_Dict is used will report an error. Some operators cannot find the name. For example:

if backbone == "mobilenet":
    self.backbone = mobilenet()
    flat_shape = 1024
    elif backbone == "inception_resnetv1":
    self.backbone = inception_resnet()
else:
    raise ValueError('Unsupported backbone - `{}`, Use mobilenet, inception_resnetv1.'.format(backbone))
    self.avg = nn.AdaptiveAvgPool2d((1,1))
    self.Bottleneck = nn.Linear(flat_shape, embedding_size,bias=False)
    self.last_bn = nn.BatchNorm1d(embedding_size, eps=0.001, momentum=0.1, affine=True)
    if mode == "train": # Judgment condition, test without loading full connection
        self.classifier = nn.Linear(embedding_size, num_classes)

The strict = false option can be added to avoid operators not called in the network:

model2.load_state_dict(state_dict2, strict=False)

[Solved] ufunc ‘add‘ did not contain a loop with signature matching types (dtype(‘<U32‘), dtype(‘<U32‘))

Error type:
UFUNC 'Add' did not contain a loop with signature matching types (dtype ('& lt; U32'), dtype ('& lt; U32') - & gt; dtype('<U32')

The ‘Add’ function is a self-defined addition function. The error type is translated as: UFUNC ‘Add’ does not contain a loop with signature matching type. Check the error causes of others. Most of them are due to data type mismatch. The following add function will report this error when x is of type int and y is of type str.

def add(x,y):
    print(type(x))
    print(type(y))
    return x+y

Therefore, I added a function to view the data type in the add function, and found that the data type is:

<class 'numpy.float64'>
<class 'list'>

This confused me. I rechecked my function again

df1["property_grid"]=df1[["wgs84_lon","wgs84_lat"]].apply(lambda x: add( x["wgs84_lon"], ["wgs84_lat"]),axis=1)

It is found that an X is missing. The correct is as follows:

df1["property_grid"]=df1[["wgs84_lon","wgs84_lat"]].apply(lambda x: add( x["wgs84_lon"], x["wgs84_lat"]),axis=1)

Perfect solution~

[Solved] ValueError: row index was 65536, not allowed by .xls format

1. Cause analysis

Xlrd and xlwt are functions used to process XLS files in Python. The maximum number of rows in a single sheet is 65535. Therefore, this error occurs when the amount of read and write data exceeds: valueerror: row index was 65536, not allowed by xls format

2.Solution

1. Method 1

The xlwt library is still used for processing, but it limits the maximum number of 5W rows of data in each sheet

# Note: What needs to be written is the two-dimensional list target_data
# Write data into excel table
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet("Sheet1")
sheet2 = workbook.add_sheet("Sheet2")
sheet3 = workbook.add_sheet("Sheet3")
al = xlwt.Alignment()
al.horz = 0x02 # Set horizontal center
# Create a style object, initialize the style
style = xlwt.XFStyle()
style.alignment = al # set horizontal center

print(len(target_data)) # Look at the length of target_data data, by checking that there are actually 14W rows, so we split into 3 sheets

for i in range(0,50000):
    for j in range(0,len(target_data[i])):
     sheet1.write(i, j, target_data[i][j], style)

for i in range(50000,100000):
    for j in range(0,len(target_data[i])):
     sheet2.write(i-50000, j, target_data[i][j], style)

for i in range(100000,len(target_data)):
    for j in range(0,len(target_data[i])):
     sheet3.write(i-100000, j, target_data[i][j], style)

# Set the width of each column
sheet1.col(0).width=256*15
sheet1.col(1).width=256*15
sheet1.col(2).width=256*15
sheet1.col(3).width=256*15
sheet2.col(0).width=256*15
sheet2.col(1).width=256*15
sheet2.col(2).width=256*15
sheet2.col(3).width=256*15
sheet3.col(0).width=256*15
sheet3.col(1).width=256*15
sheet3.col(2).width=256*15
sheet3.col(3).width=256*15

workbook.save("target_data1220.xls") # 保存到target_data.xls

2. Method 2

Using openpyxl library, the maximum number of rows that can be processed reaches 1048576

# Description: need to write is a two-dimensional list target_data
# Write data to excel table
workbook = openpyxl.Workbook() 
sheet0 = workbook.create_sheet(index=0) # create sheet0
sheet0.column_dimensions['A'].width=15 # set the width of column A
sheet0.column_dimensions['B'].width=22 # Set the width of column B
sheet0.column_dimensions['C'].width=15 # Set the width of column C
sheet0.column_dimensions['D'].width=15 # Set the width of column D
# Write data in a loop, centered and aligned
for i in range(len(target_data)):
    for j in range(len(target_data[i])):
        sheet0.cell(i+1,j+1).value = target_data[i][j] # write data
        sheet0.cell(i+1,j+1).alignment = Alignment(horizontal='center', vertical='center') # center alignment
workbook.save('test.xlsx') # Save the file

[Solved] jupyter notebook Error: ModuleNotFoundError: No module named jupyter_nbextensions_configurator

Problem description

Platform: Windows 10 professional edition, anaconda3

When starting the Jupiter notebook, there is an error message, as follows:

ModuleNotFoundError: No module named  jupyter_nbextensions_configurator

Although the jupyter lab can still be used when it is opened, the error message is always a hidden danger. Therefore, after searching the data, the following solutions are found

Solution:

python -m pip install --user jupyter_contrib_nbextensions

#jupyter contrib nbextension install --user --skip-running-check

python -m pip install --user jupyter_nbextensions_configurator

#jupyter nbextensions_configurator enable --user

Only running the above two commands solves the problem of error reporting.

[Solved] Pychar error: modulenotfounderror: no module named ‘requests_ HTML ‘solution

**About pychar’s error modulenotfounderror: no module named ‘requests_HTML ‘
code from requests_HTML import htmlsession is marked with red and an error is reported

Error reason

Pycharm did not import requests_HTML Library

Solution:

    1. 1.file — settings

    1. 2.project: pycharmprojects — Python interpreter. Above a long list of libraries displayed on the right, there is a + – △ visual symbol. Click the + sign

    1. 3.enter the added Library: requests HTML in the input box, and click Install Package in the lower left corner to install it. I have already installed it here, So the button turns gray

    1. 4.returns to the previous page. You can see the installed requests HTML package on the right. Click OK

    5. The code is not marked red yes

[Solved] Windows10 Pycharm Use Virtual Environment Error: Cannot set up a python SDK

Solution:

Configure environment variables for virtual environment

The path is the path of the Python interpreter in the virtual environment
in Python, file => settings=> project interpreter=> add=> System
interpreter, select the python path of the above virtual environment, click OK, and no more errors will be reported



[Solved] Jupyter notebook use pyLDAvis Error: modulenotfounderror: no module named ‘pyLDAvis’‘

Background of the problem

Getting started with Python
try to use pyldavis for simple topic extraction;

Problems and related codes

Pyldavis has been installed, as shown in the figure (Annotated):

but an error occurs when entering the following statement:
error code segment:

import pyLDAvis
import pyLDAvis.sklearn
pyLDAvis.enable_notebook()
pyLDAvis.sklearn.prepare(lda,tf,tf_vectorizer)

Screenshot of specific error reporting:

Solution:

Some bloggers suggested that the installation was not successful. I installed it as an administrator. I operated it, but it was useless
in fact, the final solution is very simple. I found that the kernel used by my Jupiter notebook is wrong. I used a virtual environment I set up before. Just change it to the default environment (my one is named python3).

[Solved] apex Xavier install torchvision error: illegal instruction

Description: I installed torch vision 0 eight

git clone -b v0.8.1 https://github.com/pytorch/vision.git vision-0.8.1

Installation:

cd vision-0.8.1
sudo /home/nvidia/mambaforge/envs/ultra-fast-lane/bin/python3.6 setup.py install

Note: be sure to make the path and sudo

Error: illegal instruction

Solution: reduce the numpy version. I use numpy = 1.19.3. Success