Category Archives: Python

Ubuntu18.04 (Python3.9) Error: ImportError: Cannot import name ‘sysconfig’ from ‘distutils’

1. Error reporting when using PIP:

 1 Traceback (most recent call last):
 2   File "/usr/bin/pip3", line 9, in <module>
 3     from pip import main
 4   File "/usr/lib/python3/dist-packages/pip/__init__.py", line 14, in <module>
 5     from pip.utils import get_installed_distributions, get_prog
 6   File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in <module>
 7     from pip.locations import (
 8   File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
 9     from distutils import sysconfig
10 ImportError: cannot import name 'sysconfig' from 'distutils‘

2. Solution:

2.1 installation pip

1 sudo apt-get install python3-pip

2.2 installing distutils

1 sudo apt-get install python3. 9-distutils

PS:Python3. For versions after 5, when installing distutils, you should install it with a specific version

sudo apt-get install python3.X-distutils

3. Installation succeeded

After all installations are successful, PIP – V can run normally and use pip.

[Solved] Python Error: ModuleNotFoundError : No Module named ‘pip._internal’

1. Error reporting when using PIP:

1 Traceback (most recent call last):
2   File "/usr/local/bin/pip", line 7, in <module>
3     from pip._internal import main
4 ImportError: No module named _internal

2. Solution:

Update pip

python3 -m pip install --upgrade pip
pip install --upgrade pip

3. Completion

After all updates are installed successfully, PIP – V can run normally and use pip.

[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