Category Archives: Python

[Solved] Python3 operate pymongo error: pymongo.errors.ConfigurationError

It is the first time to use Python 3 to connect Mongo Library in MAC.

1. Confirm whether python3 has been installed

which python3

2. Install pymongo

sudo pip3 install pymongo

3. Use Python script

python3 demo.py

Result error:

pymongo.errors.ConfigurationError: Unknown option username

I checked for a long time and didn’t find an accurate solution. Later, check the version of pymongo in the environment that can run normally before on the original window.

Resolutely upgrade pymongo to version 3.12. Problem solving.

sudo pip3 install pymongo==3.12

python=2.7-not available from current channels [How to Solve]

phenomenon

Creating a python 2 environment using minicanda3

Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed

PackagesNotFoundError: The following packages are not available from current channels:

  - python=2.7

Current channels:

  - https://repo.anaconda.com/pkgs/main/osx-arm64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/osx-arm64
  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

Cause location

Python 2 official has stopped maintenance and updating.

There are no python2 related packages in the official warehouse

CONDA default channel failed to retrieve python2 7 lead

 

Solution:

From the configuration of anaconda2, find the available channels: https://repo.continuum.io/pkgs/free/osx-64

Specify the channel URL to update the environment

conda create -c 'https://repo.continuum.io/pkgs/free/osx-64' -n py2 python=2.7

[Solved] python AudioSegment Read mp3 files Error: Python AudioSegment winError 2 The system cannot find the file specified

Today, run a sound mixing program using Python’s audiosegment from_Mp3 (mp3path) reports an error

Google said that the file ffmpeg is missing Exe and ffprobe Exe, and then I downloaded windows: https://github.com/BtbN/FFmpeg-Builds/releases?page=3

I use Python 3 6. I downloaded several

Use the first option:

Add directly to the code

    AudioSegment.converter = r"E:\s\ffmpeg-N-104843-gf176669a37-win64-lgpl-shared\ffmpeg-N-104843-gf176669a37-win64-lgpl-shared\bin\ffmpeffmpeg.exe"
    AudioSegment.ffprobe = r"E:\s\ffmpeg-N-104843-gf176669a37-win64-lgpl-shared\ffmpeg-N-104843-gf176669a37-win64-lgpl-shared\bin\ffprobe.exe"
    

Then run, fail, try, and all three fail…

Using the second scenario:

Directly copy the two exe files to the project running directory, and the third one failed.

Then use the two exe files in the second one successfully!!! I almost gave up ~ I succeeded

Another problem: there is a sound merging function in this folder, which can be called internally and run, but it can’t be adjusted for other files (different folders) to call this function, even if they don’t pass parameters…

Then I copy the two exe files to the folder where the PY file is located before I can run… I don’t know why…

Attached:

Audio mix (not splice):

    sound1 = AudioSegment.from_mp3("E:\\s\\CodeEditer\\1.mp3")
    sound2 = AudioSegment.from_mp3("E:\\s\\CodeEditer\\2.mp3")
    combined = sound1.overlay(sound2)
    combined.export("3.mp3", format='mp3')

 

[Solved] Python request Error: requests.exceptions.ReadTimeout: HTTPSConnectionPool (xxxx)

Screenshot of the problem:

Mental process

First, the request parameters are determined. The URLs are consistent, but an error is reported

Method 1:

import requests

requests. get(‘ https://www.google.com/ ‘,verify=False)

If it’s not solved, it’s not the agent’s reason

Method 2:

session = requests. session()

session. keep_ Alive = false # close redundant connections

Using session error reporting session request

It still hasn’t been solved. Generally, it can be solved at this step, but an error is still reported when executing python

Finally, I found that I was out of my mind. Since the header request header directly copies other items, just annotate the “content length”: “143” in it. Only remember that the header in the later step is better to use the header of the current interface. You can delete redundant values yourself. It’s not convenient to pull an item from other items, which will increase the troubleshooting time

[Solved] Django Error: – no such table: main.auth_user__old

Django reports an error no such table: main auth_user__old

 

An error is reported when adding an article in the admin background

 

Django 2.0, I checked it. The reason for the error is that Django version is low and SQLite3 database is compatible

 

Solution:

Upgrade Django to 2.1.0 five

pip install django==2.1. 5 

Just enter the command directly, and version 2.0 will be uninstalled automatically.

 

 

Delete migrations folder

 

Delete SQLite database

 

 

Migrate the default app first

python manage.py migrate

Then migrate your own app myblos

python manage.py makemigrations myblogs
python manage.py migrate myblogs

 

 

Re create administrator account and password

Just go back in and add the article

[Solved] DeprecationWarning: the imp module is deprecated in favour of importlib

DeprecationWarning: the imp module is deprecated in favour of importlib

The reason for this problem is that the imp library is abandoned after Python 3.4 and the importlib library is used, so we can change the source file of our compiler pycham

In this directory, C:\pycharm 5.0 4\helpers\pycharm (check according to your installation path) has two files

1. docrunner.py

2. utrunner.py

1. Comment out imp, import importlib

2. Comments imp.load_Source, use importlib machinery.Sourcefileloader load module

#import imp
import importlib


#module = imp.load_source(moduleName, fileName)
module = importlib.machinery.SourceFileLoader(moduleName, fileName).load_module()

[Solved] Python virtual environment install Error: returned non zero exit status 1

The reason is unknown, so the installed PIP version is a little messy

 

 

My actions:

-Install virtual environment: Python 3 – M venv — without PIP env

-Switch to virtual environment: source env/bin/activate

-Get PIP installation file: WGet https://bootstrap.pypa.io/get-pip.py

-Install PIP: Python get pip py

-Exit virtual environment: deactivate

-Re enter the virtual environment: source env/bin/activate

-Confirm Python: which Python

-Confirm PIP: which PIP

[Solved] SSH Remote execute Python 3 error: Unicode encodeerror: ‘ASCII’ codec

background

Recently, the project requires SSH to execute Python code remotely on the centos7 container. The python version is 3.6. When opening a file with a Chinese name, there will be no problem executing Open ('wolf.TXT') locally on the centos7 container. However, after connecting through SSH, it is found that the open() method reports the following error:

UnicodeEncodeError :'ascii' codec can't encode characters in position 0-2

The problem occurs when opening.TXT (Chinese file name) is used as a parameter to encode according to the ASCII encoding method. We know that the ASCII encoding contains only 128 bits, including numbers, uppercase and lowercase letters and some special symbols, and does not contain Chinese characters. [this article will not introduce why encode is used]

Therefore, the code can run normally by making the following changes:

open('Wolf_is_coming.txt'.encode('utf8'))

Because here we specify the encoding format of UTF-8 (of course, it needs to be supported by the running environment).

Find out the essence of the problem, and then we can look at the SSH problem.

Language coding loop mirror

Python runtime container environment

In the python runtime environment container, use the locale and locale - a commands to view and discover the language coding environment, as shown below:

[root@pyrun-test-69d4d45d79-mqg6n /]# locale -a
C
en_US.utf8
POSIX
[root@pyrun-test-69d4d45d79-mqg6n /]# locale 
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

Language coding environment of SSH client

The language coding environment of SSH client is as follows:

root@coding-editor-test-8c6cdfdd8-9tpcl:/app# locale -a
C
C.UTF-8
POSIX
root@coding-editor-test-8c6cdfdd8-9tpcl:/app# locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=

Configure language encoding environment

Through comparison, it is found that the coding environment of SSH client is c.utf-8, while the python running environment is en_US.utf8

(PS: C represents ASCII code, and en_us.utf8 and zh_cn.utf8 contain Chinese characters)

The first thought is that the SSH client may affect the default encode mode of Python runtime, so we set the SSH client environment to the same “here the client is Ubuntu” as the python runtime environment:

#!/bin/sh

# 1.set the language as en_US_UTF-8
echo -e 'LANG="en_US_UTF-8"\nLANGUAGE="en_US:en"' >> /etc/default/locale
# Effective Configuration
source /etc/default/locale

# 2.If the en_US_UTF-8 language pack is missing install the locales tool and set en_US_UTF-8
apt-get install --no-install-recommends -y locales
locale-gen en_US.UTF-8
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8

After setting, try SSH to the python running environment container again. It is found that Open ('wolf.TXT') runs without any error.

So the question is, why does SSH run in the client’s coding environment

Research on SSH env mechanism

cat /etc/ssh/ssh_config 

The following contents are found in the SSH configuration file:

The above configuration will send the local locale sendenv to the python runtime environment, so we don’t need to ensure that the locale of the client and the python runtime environment are consistent.

You only need to modify the configuration to:

SendEnv LANG en_US.utf8

Mina SSH settings

Since the project is the code that Java calls the python running environment terminal through Mina SSH to execute, rather than SSH directly, The modification of /etc/SSH/SSH_config does not solve the problem.

Through the observation of Mina, it is found that parameters can be passed when creating a channelshell. The source code interface is as follows:

/**
 * Create a channel to start a shell using specific PTY settings and/or environment.
 *
 * @param  ptyConfig   The PTY configuration to use - if {@code null} then internal defaults are used
 * @param  env         Extra environment configuration to be transmitted to the server - ignored if
 *                     {@code null}/empty.
 * @return             The created {@link ChannelShell}
 * @throws IOException If failed to create the requested channel
 */
ChannelShell createShellChannel(
  PtyChannelConfigurationHolder ptyConfig, Map<String, ?> env)
  throws IOException;

When creating a channelshell, you can write this to configure the language coding:

shellChannel = session.createShellChannel(new PtyChannelConfiguration(), Map.of("LANG","en_US.utf8"));

[Solved] office Auto Error: ‘win32com.gen_py….’ has no attribute ‘CLSIDToClassMap’

D4e67c7b7089 error code:

Import Win32 com.client as Win32
word = win32.gencache.ensuredispatch (‘word. Application ‘) ## error location
error prompt:

AttributeError: module ‘win32com.gen_Py. 00020905-0000-0000-c000-000000000046x0x8x5 ‘has no attribute’ clsidtoclassmap ‘
solution: delete the cache folder in the directory  C:\users\Erica\appdata\local\temp\Gen_00020905-0000-0000-c000- 000000000046x0x8x5 in py\3.7 can be used. Re execute the above code and no error will be reported.

Tip: use everything to search Gen_Py, confirm the folder Gen_Py location.

How to Solve Python Import skimage Error

Background
I recently used skimage for image processing and found an error when quoting
ImportError: DLL load failed while importing _rolling_ball_cy: The specified module could not be found.
Environment
Windows 10
Python 3.9