Category Archives: Python

[Solved] ‘utf-8’ codec can’t decode byte 0xd4 in position 1,invalid continuation byte

Error while reading file. This is an encoding problem that python is very prone to encounter when reading files. The solution is to add encoding=’ISO-8859-1′.

 

 

 

 

Since the default ftp download is ascii mode, the downloaded file encoding is iso8859-1.

Python: How to Calculate the Distance of Latitude and Longitude

 1     def geodistance(self, lng1, lat1, lng2, lat2):
 2         # lng1,lat1,lng2,lat2 = (120.12802999999997,30.28708,115.86572000000001,28.7427)
 3         lng1, lat1, lng2, lat2 = map(radians, [float(lng1), float(lat1), float(lng2), float(lat2)])  # Latitude and longitude to radians
 4         dlon = lng2 - lng1
 5         dlat = lat2 - lat1
 6         a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
 7         distance = 2 * asin(sqrt(a)) * 6371 * 1000  # Average radius of the Earth, 6371km
 8         # Unit: meter
 9         distance = round(distance, 3)
10         return distance

[Solved] pycharm+selenium Build Environment Error: no module named ‘selenium’

1. After the installation is successful through the pip command in cmd, check the installation success in the command line

2. Running the program in pycharm still prompts the error

Set as shown below

3. The python location on the first line of the runner console needs to be configured in the setting

 

4. Setting configuration The path of the location shown in the figure below is the same as the console output.

 

 

After successful execution

[Solved] jenkins Build python Program Error: ModuleNotFoundError: No module named ‘htmltestreport’

Background:

The project is built based on virtual environment, and the third-party packages are actually installed in:

E:\PyProject\py_basic\Lib\site-packages is not the same directory as Python installation directory

Question:

Both pycham and CMD can operate normally, but Jenkins build error:

C:\Users\86158\.jenkins\workspace\ihrm_code>python run_suite.py 
Traceback (most recent call last):
  File "run_suite.py", line 3, in <module>
    from htmltestreport import HTMLTestReport
ModuleNotFoundError: No module named 'htmltestreport'

Solution:

Add environment variables to Jenkins. This problem is usually caused by the configuration of environment variables

[Solved] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

Environment pycharm2021.1.1+python3.7+django3.2.11

When testing the project, I opened the python console and got django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.

The driver is fine, the database configuration is fine, and __init__.py has added this

import pymysql

pymysql. install_as_MySQLdb()

Solution: PIP – M install mysqlclient

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

pip install software error: Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-*(where x is related to the software to be installed)
e.g. pip install pyparsing==1.5.7 install Error: Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-WImLdR/pyparsing/
Solution:
sudo python -m pip install –upgrade –force pip
sudo pip install setuptools==40.8.0

[Solved] Azure Python SDK Error: The resource principal named https://management.azure.com was not found in the tenant China Azure

Problem description

When using the python SDK, when logging in to China azure (mooncake) and accessing the alertsmanagement resources, you often encounter the error message “environmentcredential: authentication failed”.

Python code:

from azure.identity import DefaultAzureCredential
from azure.mgmt.alertsmanagement import AlertsManagementClient

# Acquire a credential object using CLI-based authentication.
credential = DefaultAzureCredential()
subscription_id = "xxxx-xxxx-xxxx-xxxx-xxxx"

alertClient = AlertsManagementClient(credential,subscription_id,base_url="https://management.chinacloudapi.cn/")

rules = alertClient.smart_detector_alert_rules.list()
for rule in rules:
    print("Rule Name: " + rule.name)

Error message:

PS C:\LBWorkSpace\MyCode\46-alertrule-python> python getrule.py
DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
        EnvironmentCredential: Authentication failed: AADSTS500011: The resource principal named https://management.azure.com was not found in the tenant named xxx Mooncake. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.
Trace ID: xxxxxxxx-xxxx-xxxx-xxxx-9e130dbf7900
Correlation ID: xxxxxxxx-xxxx-xxxx-xxxx-46769c9e1e10
Timestamp: 2022-01-27 12:09:35Z
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.
Traceback (most recent call last):
  File "C:\LBWorkSpace\MyCode\46-alertrule-python\getrule.py", line 15, in <module>
    for rule in rules:
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\paging.py", line 129, in __next__
    return next(self._page_iterator)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\paging.py", line 76, in __next__
    self._response = self._get_next(self.continuation_token)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_redirect.py", line 158, in send
    response = self.next.send(request)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_retry.py", line 445, in send
    response = self.next.send(request)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_authentication.py", line 117, in send
    self.on_request(request)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_authentication.py", line 94, in on_request
    self._token = self._credential.get_token(*self._scopes)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\identity\_credentials\default.py", line 172, in get_token
    return super(DefaultAzureCredential, self).get_token(*scopes, **kwargs)
  File "C:\Users\bulu\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\identity\_credentials\chained.py", line 108, in get_token
    raise ClientAuthenticationError(message=message)
azure.core.exceptions.ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
        EnvironmentCredential: Authentication failed: AADSTS500011: The resource principal named https://management.azure.com was not found in the tenant named xxxx Mooncake. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.
Trace ID: xxxxxxxx-xxxx-xxxx-xxxx-9e130dbf7900
Correlation ID: xxxxxxxx-xxxx-xxxx-xxxx-46769c9e1e10
Timestamp: 2022-01-27 12:09:35Z
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.

Problem-solving:

From the error message https://management.azure.com, we know that the problem is due to the default value of Resource Principal used in AlertsManagementClient, which was not changed to base_url with the specification of

https://management.chinacloudapi.cn/ . The problem can be mitigated by specifying credential_scopes as [“https://management.chinacloudapi.cn/.default”] when constructing the AlertsManagementClient object.
The modified code is :

# pre:
alertClient = AlertsManagementClient(credential,subscription_id,base_url="https://management.chinacloudapi.cn/")

# new:
alertClient = AlertsManagementClient(credential,subscription_id,base_url="https://management.chinacloudapi.cn/",credential_scopes=["https://management.chinacloudapi.cn/.default"])

PS: when creating client objects of other resources, if you encounter the same principal problem, you can set credential_Scopes to solve the problem.

The complete code that can travel far is:

# Import the needed credential and management objects from the libraries.
from azure.identity import DefaultAzureCredential
from azure.mgmt.alertsmanagement import AlertsManagementClient

# Acquire a credential object using CLI-based authentication.
credential = DefaultAzureCredential()
subscription_id = "a9dc7515-7692-4316-9ad4-762f383eec10"

# # pre:
# alertClient = AlertsManagementClient(credential,subscription_id,base_url="https://management.chinacloudapi.cn/")
# Modified:
alertClient = AlertsManagementClient(credential,subscription_id,base_url="https://management.chinacloudapi.cn/",credential_scopes=["https://management.chinacloudapi.cn/.default"])



rules = alertClient.smart_detector_alert_rules.list()
for rule in rules:
    print("Rule Name: " + rule.name)

Operation results:

The correct MonitorManagementClient object to get metric_alerts and activity_log_alerts Get Alert Rule code

from azure.mgmt.monitor import MonitorManagementClient
from azure.identity import DefaultAzureCredential
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD as CLOUD
import os


os.environ["SUBSCRIPTION_ID"] = "xxxxxxyour-subidxxxxxx"
os.environ["AZURE_TENANT_ID"] = "your tenant idxxxxx"
os.environ["AZURE_CLIENT_ID"]  = "client_id_sp"
os.environ["AZURE_CLIENT_SECRET"]  = "pw_sp"
subscription_id = os.environ["SUBSCRIPTION_ID"]

credential = DefaultAzureCredential(authority=CLOUD.endpoints.active_directory)

# create client
client1 = MonitorManagementClient(
    credential,
    subscription_id,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"]
)

#classic
my_alerts1 = client1.alert_rules.list_by_subscription()

for j in my_alerts1:
    print(j)

#log search alerts
client2 = MonitorManagementClient(
    credential,
    subscription_id,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"]
)
my_alerts2 = client2.scheduled_query_rules.list_by_subscription()
for j in my_alerts2:
    print(j)

#activity alerts
client3 = MonitorManagementClient(
    credential,
    subscription_id,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"],
    api_version="2017-04-01"
)

my_alerts3 = client3.activity_log_alerts.list_by_subscription_id()
for j in my_alerts3:
    print(j)

#metric alerts
client4 = MonitorManagementClient(
    credential,
    subscription_id,
    base_url=CLOUD.endpoints.resource_manager,
    credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"]
)

my_alerts4 = client4.metric_alerts.list_by_subscription()
for j in my_alerts4:
    print(j)

Outcome:

[Solved] ImportError: ERROR: recursion is detected during loading of “cv2” binary extensions. Check OpenCV installation.

Error:
pyinstaller package and run exe error: “recursion is detected during loading of “cv2“ binary extensions.”
Traceback (most recent call last):
  File "Sy.py", line 15, in 
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "cv2\__init__.py", line 180, in 
    bootstrap()
  File "cv2\__init__.py", line 152, in bootstrap
    native_module = importlib.import_module("cv2")
  File "importlib\__init__.py", line 126, in import_module
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "cv2\__init__.py", line 180, in 
    bootstrap()
  File "cv2\__init__.py", line 75, in bootstrap
    raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.

========================

Use pip to uninstall opencv, pip uninstall opencv ******* (the name of the specific opencv installation)
Go to the %:\python\Lib\site-packages% path (the path of the python installation) and delete the cv2 folder
Reinstall opencv with pip, pip install opencv****

========================

Solution (you can try in sequence).

1. reinstall cv2. (This is the answer more, most cases useful)

first pip unintall opencv-python , then pip intall opencv-python

2. set the environment variables.

3. lower cv2 version.

There is a compatibility problem between pyinstaller and cv2 version. I use pyinstaller is 4.7 (python3.8.0), cv2 is 4.5.4.58, the result is an error, lower the cv2 version to 4.5.1.48 and then package, it does not report an error.

========================

https://wiki.archlinux.org/index.php/Arch_Linux_Archive#How_to_downgrade_one_package

========================

open cmd and use pip to install a different version:

 pip install opencv-python==4.5.3.56

========================

opencv-python      4.5.5.62

========================