Install a python library in python virtual environment venv: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified

Error: When running pip install in venv, on Windows 10, the following error is displayed:

WARNING: Failed to write executable - trying to use .deleteme logic
ERROR: Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified: 'C:\Python311\Scripts\google-oauthlib-tool.exe' -> 'C:\Python311\Scripts\google-oauthlib-tool.exe.deleteme'

Solution: run

py -m pip install [the package you want to install]

Script cannot be loaded because running scripts is disabled on this system. For moreinformation, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

Error: when running the activation script for my python virtual environment, the I had the following error on Win10

Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

Solution: Open a powershell as admin and run the following command

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

hit Y and press the Enter key.

Google Cloud Vision Set Up on Windows. google module not found in virutalenv

Issue:
The following error is displayed when trying to import google vision in the virtualenv after following the installation process.

Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'google'

Google cloud installation instruction:

https://cloud.google.com/python/docs/reference/vision/latest#installation

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-vision

Solution:

To run python in the virtual environment, use the python command instead of python3

Python sort dictionary by value

Source:https://stackabuse.com/how-to-sort-dictionary-by-value-in-python/

Method 1

dict1 = {1: 1, 2: 9, 3: 4}
sorted_values = sorted(dict1.values()) # Sort the values
sorted_dict = {}

for i in sorted_values:
    for k in dict1.keys():
        if dict1[k] == i:
            sorted_dict[k] = dict1[k]
            break

print(sorted_dict)

Method 2

dict1 = {1: 1, 2: 9, 3: 4}
sorted_dict = {}
sorted_keys = sorted(dict1, key=dict1.get)  # [1, 3, 2]

for w in sorted_keys:
    sorted_dict[w] = dict1[w]

print(sorted_dict) # {1: 1, 3: 4, 2: 9}

Python: sort an array of tuple by the second element value

Note that it works equally well with an array of arrays.

# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
	return val[1]

# list1 to demonstrate the use of sorting
# using second key
list1 = [(1,2),(3,3),(1,1)]

# sorts the array in ascending according to
# second element
list1.sort(key=sortSecond)
print(list1)

# sorts the array in descending according to
# second element
list1.sort(key=sortSecond,reverse=True)
print(list1)

Source: https://www.geeksforgeeks.org/sort-in-python/

Python environment

To better control the development environment of your python project and ensure that your older project don’t break when updating packages, it is recommended to create a dedicated environment for your project.

For that purpose you can use python virtual environment.

Create environment:

python3 -m venv env

Activate environment:

source env/bin/activate

Numbers loaded as text when opening csv in openoffice calc

Issue: the numbers are loaded as text, when a cell containing such a number is selected, it shows that the number is preceded by a single quote and a space. search for the quote and replace it by nothing does not work.

Solution: When opening the file in open office make sure that detect special number is enabled

If your decimal separator is a dot, then the number might be changed into a date, turn it off and replace the dot by a coma when the csv is open.

Also the csv file was created with python using utf8-sig which had a BOM each time it happened something to the file. Encoding the file using utf-8 only solved the issue.

Issue remains when you have both number separated by a dot that can be change into a date and integer.