Tag Archives: Python

[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

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

[Solved] Python Error: UnicodeDecodeError: ‘gb2312’ codec can’t decode byte 0xa4 in position… : illegal multibyte sequence

1. Error reporting scenario

An error is reported when using Python: “Unicode decodeerror: ‘GB2312’ codec can’t decode byte 0xa4 in position…: illegal multibyte sequence” generally has the following two scenarios:

1. Crawl Chinese website content

html = requests.get(url).decode("gb2312")

2. Read GBK encoded files

result = open(filename, 'r', encoding='gb2312')

2. Error reporting reason

The Chinese character set included in GB2312 is not comprehensive enough, and decoding errors will occur when encountering traditional characters.

Chinese character set range GB2312 < gbk < gb18030

3. Error reporting solution

1. Ignore decoding errors

html = requests.get(url).decode('gb2312',errors = 'ignore')

The default parameter of the decode function is strict. Decode ([encoding], [errors =’strict ‘]). You can use the second parameter to control the error handling strategy. Strict means that an exception is thrown when an illegal character is encountered
if it is set to ignore, illegal characters will be ignored
if it is set to replace, it will be used? Replace illegal characters

2. Replace gbk2312 with GBK with a more comprehensive Chinese character set

result = open(filename, 'r', encoding='gbk')

Note: if ‘ignore’ is used to ignore illegal characters and report errors, the read Chinese will be garbled. If you want to read accurate Chinese content, you can first convert the content encoded in GB2312 to UTF-8 and then read it.

res = requests.get(url)
res = decode(res, "gb2312").encode("utf8")
res.encoding = 'utf-8'
html = res.text
print(html)

At this time, Chinese characters can be output normally.

Python — the copied virtual environment PIP command failed to run. Fatal error in launcher: unable to create process using

Original text: https://blog.csdn.net/weixin_ 39278265/article/details/82938270

You need to modify the directory referenced in the pip.exe file in the scripts directory of the virtual environment.

Referring to the original text, I used Notepad + + to open pip.exe, and then searched according to the python path information prompted,

After finding it, replace it with the path of python.exe in the current virtual environment and save it.

Python: How to Modify AD Password

Prerequisite: AD has started certificate service (the most important sentence).

import ldap3

SERVER = 'adserver'
BASEDN = "DC=example,DC=com"
USER = "[email protected]"
CURREENTPWD = "adcvQ.SAD"
NEWPWD = "adcv.Q.SAD"

SEARCHFILTER = '(&(userPrincipalName='+USER+')(objectClass=person))'

USER_DN = ""
USER_CN = ""

ldap_server = ldap3.Server(SERVER, get_info=ldap3.ALL, use_ssl=True)
conn = ldap3.Connection(ldap_server, USER, CURREENTPWD, auto_bind=True)
conn.start_tls()

conn.search(search_base=BASEDN, search_filter=SEARCHFILTER, search_scope=ldap3.SUBTREE, attributes=['cn', 'givenName', 'userPrincipalName'], paged_size=5)

# print(conn.response)
for entry in conn.response:
    if entry.get("dn") and entry.get("attributes"):
        if entry.get("attributes").get("userPrincipalName"):
            if entry.get("attributes").get("userPrincipalName") == USER:
                USER_DN = entry.get("dn")
                USER_CN = entry.get("attributes").get("cn")


if USER_DN:
    res = ldap3.extend.microsoft.modifyPassword.ad_modify_password(conn, USER_DN, NEWPWD, CURREENTPWD,  controls=None)
    if res:
        print('user %s change password Success.' % USER_CN)
    else:
        print('user %s change password Failed.' % USER_CN)
else:
    print("User DN is missing!")

Python Via get-pip.py Install pip Error: zipimport.ZipImportError: can‘t decompress data; zlib not availabl

How to Solve error: zipimport.ZipImportError: can‘t decompress data; zlib not availablwget https://bootstrap.pypa.io/pip/2.7/get-pip.py  # python2.7
wget https://bootstrap.pypa.io/get-pip.py  # python3
error:

[[email protected] ~]$ https://bootstrap.pypa.io/get-pip.py
[[email protected] ~]$ sudo python get-pip.py

The error message “zipimport.ZipImportError: can’t decompress data; zlib not available”
It seems that the zlib library is missing and needs to be installed before execution.
Problem solved

1 [[email protected] ~]$ sudo yum install zlib

After the installation was completed, we found that it still reported an error, and a Google search revealed that we needed to install zlib-dev
So we install the zlib library again

1 [[email protected] ~]$ sudo yum install zlib*

This time the installation should be OK, but the result is disappointing, it still gives the same error.
But we have already installed the zlib library, why does it still give us an error?

We need to recompile and install python
Before recompiling, we need to modify the Modules/Setup.dist file in the installation source file to change the

1 #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

The comment on this line is removed and becomes

1 zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

Then compile and install it again (execute the following command in Python’s installation source directory)

1 [[email protected] ~]$ make && make install

Reinstallation completed
Execute.

1 [[email protected] ~]$ sudo python get-pip.py

Install pip successfully!

[Solved] Python Error: TypeError: ‘dict_keys’ object does not support indexing

Error message:

When learning the book machine learning practice, I ran according to the code in the book and made an error, but there was no error prompt in the code. The error codes are as follows:

firstStr = myTree.keys()[0]
print('树的叶子结点个数为:\n{}'.format(getNumLeafs(myTree)))

Errors are reported as follows:

firstStr = myTree.keys()[0]
TypeError: 'dict_keys' object does not support indexing

Type error: Dict_Keys object does not support indexing

The error is caused by different versions. The author uses version 2.X, while I use version 3.7.X.

Solution

For version 3.x, because python3 changed dict.keys, which returns a dict_keys object that supports iterable but not indexable, we can explicitly convert it to a list

Modify the error code as follows:

firstSides = list(myTree.keys()) 
firstStr = firstSides[0] 
# or 
firstStr = list(myTree.keys())[0] 

Just run it now.