Category Archives: Python

[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.

Python pip install three-party library unsuccessful error: WARNING: You are using pip version 20.2.3; however, version 20.2.4 is available

Originally, I wanted to download the requests module. As a result, PIP reported an error during installation. After solving the error problem, I installed the requests module to verify whether the problem was successfully solved

1. Failed to install the third-party library for pip

PIP failed to install the third-party library: warning: you are using PIP version 20.2.3; however, version 20.2.4 is available.
You should consider upgrading via the ‘c:\users\lenovo\appdata\local\programs\python\python37\python.exe -m pip install –upgrade pip’ command.

Cause: the installation of the third-party library failed because the PIP version is too low.
solution:
Enter: easy in CMD_install -U pip
————————————————

[Solved] Python error: attributeerror: type object ‘STR’ has no attribute ‘_name_’ (machine learning practice treeplotter code)

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:

if type(secondDict[key])._name_ == 'dict':

Errors are reported as follows:

AttributeError: type object 'str' has no attribute '_name_'

Attribute error: the type object “STR” has no attribute “name”,

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

Solution

The type object “STR” in Python 3 does not have the “_name_” attribute, so we need to remove the attribute. In addition, this judgment statement was originally used to judge whether the node type is dict (Dictionary), but because the single quotation mark (”) is added, it becomes a string, which will cause other errors, so we also need to remove the single quotation mark.

Modify the error code as follows:

if type(secondDict[key]) == dict:

Just run it now.

[Solved] PIP install selenium error: ERROR: Could not install packages due to an OSError…

Error Messages:

ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host=’files.pythonhosted.org’, port=443): Max retries exceeded with url: /packages/ad/24/39cab5fbaf425ff522e1e51cce79f94f10f9523f015d2b2251e43f45e8a2/selenium-4.0.0-py3-none-any.whl (Caused by ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x000001BEAD0A2E88>, ‘Connection to files.pythonhosted.org timed out. (connect timeout=15)’))

D:\software\python\pip-21.3.1\pip-21.3.1>pip install selenium-4.0.0-py3-none-any.whl
WARNING: Requirement ‘selenium-4.0.0-py3-none-any.whl’ looks like a filename, but the file does not exist
Processing d:\software\python\pip-21.3.1\pip-21.3.1\selenium-4.0.0-py3-none-any.whl
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: ‘D:\\software\\python\\pip-21.3.1\\pip-21.3.1\\selenium-4.0.0-py3-none-any.whl’

Solution:

pip install torchtext

Continue after installation Install selenium
pip install selenium

Pytorch dataloader Error: RuntimeError: stack expects each tensor to be equal size, but got [4] at entry 0 and [5] at entry 1

In this case, you can also find batch_ When the size is 1, no error will be reported,

batch_ If the size is greater than 1, an error will be reported because the dimensions of entries in the same batch are different

For example, batch = [[2,3,5,1], [3,4,5,2,3]]

Solution:

Make up to the same length

# Complement the length of all vectors to max_length
multi = np.pad(multi, (0, max_length-multi.shape[0]), 'constant', constant_values=(0, 0))

Note that in the dataset class__get__Supplement in item() method

[Solved] Python 3.9 Start Ride error: Attributeerror: ‘MenuItem’ object has no attribute ‘getlabel’

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/application/application.py", line 62, in OnInit
self.plugin_loader.enable_plugins()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/application/pluginloader.py", line 43, in enable_plugins
p.enable_on_startup()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/application/pluginconnector.py", line 52, in enable_on_startup
self.enable()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/application/pluginconnector.py", line 57, in enable
self._plugin.enable()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/recentfiles/recentfiles.py", line 44, in enable
self._add_recent_files_to_menu()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/recentfiles/recentfiles.py", line 114, in _add_recent_files_to_menu
self.register_action(action)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/pluginapi/plugin.py", line 204, in register_action
action = self._frame.actions.register_action(action_info)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/ui/mainframe.py", line 751, in register_action
self._menubar.register(action)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/ui/actiontriggers.py", line 60, in register
menu.add_menu_item(action)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/ui/actiontriggers.py", line 98, in add_menu_item
menu_item = self._construct_menu_item(action)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/ui/actiontriggers.py", line 107, in _construct_menu_item
menu_item = self._create_menu_item(action)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/ui/actiontriggers.py", line 139, in _create_menu_item
pos = action.get_insertion_index(self.wx_menu)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/action/action.py", line 40, in get_insertion_index
return self._insertion_point.get_index(menu)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/action/actioninfo.py", line 286, in get_index
index = self._find_position_in_menu(menu)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/action/actioninfo.py", line 296, in _find_position_in_menu
if self._get_menu_item_name(item).lower() == self._item.lower():
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/robotide/action/actioninfo.py", line 301, in _get_menu_item_name
return self._shortcut_remover.split(item.GetLabel())[0]
AttributeError: 'MenuItem' object has no attribute 'GetLabel'
OnInit returned false, exiting...
Error in atexit._run_exitfuncs:
wx._core.wxAssertionError: C++ assertion "GetEventHandler() == this" failed at /Users/robind/projects/bb2/dist-osx-py38/build/ext/wxWidgets/src/common/wincmn.cpp(475) in ~wxWindowBase(): any pushed event handlers must have been removed

Solution: 
python3.9 can install beta version
pip install -U –pre robotframework-ride
It will start normally after you had installed it.

[Solved] ModuleNotFoundError: No module named’usysconfigdata u x86 64-u-gnu’

Technical background

In the last blog post, I performed the update of CONDA and installed gxx with CONDA_After linux-64, when some PIP instructions are executed, the following error messages will be given:

$ python3 -m pip list
Traceback (most recent call last):
  File "/home/dechin/anaconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/dechin/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/__main__.py", line 29, in <module>
    from pip._internal.cli.main import main as _main
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 9, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
    from pip._internal.cli import cmdoptions
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
    from pip._internal.cli.parser import ConfigOptionParser
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/cli/parser.py", line 12, in <module>
    from pip._internal.configuration import Configuration, ConfigurationError
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/configuration.py", line 27, in <module>
    from pip._internal.utils.misc import ensure_dir, enum
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/utils/misc.py", line 42, in <module>
    from pip._internal.locations import get_major_minor_version, site_packages, user_site
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/locations/__init__.py", line 9, in <module>
    from . import _distutils, _sysconfig
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/locations/_distutils.py", line 18, in <module>
    from .base import get_major_minor_version
  File "/home/dechin/anaconda3/lib/python3.8/site-packages/pip/_internal/locations/base.py", line 14, in <module>
    site_packages = sysconfig.get_path("purelib")  # type: typing.Optional[str]
  File "/home/dechin/anaconda3/lib/python3.8/sysconfig.py", line 521, in get_path
    return get_paths(scheme, vars, expand)[name]
  File "/home/dechin/anaconda3/lib/python3.8/sysconfig.py", line 511, in get_paths
    return _expand_vars(scheme, vars)
  File "/home/dechin/anaconda3/lib/python3.8/sysconfig.py", line 172, in _expand_vars
    _extend_dict(vars, get_config_vars())
  File "/home/dechin/anaconda3/lib/python3.8/sysconfig.py", line 559, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/home/dechin/anaconda3/lib/python3.8/sysconfig.py", line 430, in _init_posix
    _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
ModuleNotFoundError: No module named '_sysconfigdata_x86_64_conda_linux_gnu'

Problem location and solution

After a round of searching, the cause of the problem is finally located as: a backup file is lost in a certain version of Python. Usually, there are two identical in the same version of Python_ sysconfigdata_ x86_ 64_ conda_ cos6_ linux_ GNU. Py files and _ sysconfigdata_ x86_ 64_ conda_ linux_ GNU. Py file. Some programs depend on one of them during operation, so what we need to do is to retrieve whether these two files exist in the system:

$ sudo find ~ -name _sysconfigdata_x86_64*
[sudo] dechin password is:
/home/dechin/anaconda3/lib/python3.8/_sysconfigdata_x86_64_apple_darwin13_4_0.py
/home/dechin/anaconda3/lib/python3.8/__pycache__/_sysconfigdata_x86_64_apple_darwin13_4_0.cpython-38.pyc
/home/dechin/anaconda3/lib/python3.8/_sysconfigdata_x86_64_conda_cos6_linux_gnu.py

Here we find that there is only one under the Python 3.8 Path currently used_sysconfigdata_x86_64_conda_cos6_linux_GNU.Py file without _sysconfigdata_x86_64_conda_linux_Gnu.py file, so we only need to make a copy of the file:

$ cp /home/dechin/anaconda3/lib/python3.8/_sysconfigdata_x86_64_conda_cos6_linux_gnu.py /home/dechin/anaconda3/lib/python3.8/_sysconfigdata_x86_64_conda_linux_gnu.py

At this time, execute the PIP instruction again:

$ python3 -m pip listPackage                            Version
---------------------------------- -----------------
absl-py                            0.13.0
adamod                             0.0.3
affinity                           0.1.0
alabaster                          0.7.12
anaconda-client                    1.8.0
anaconda-navigator                 1.10.0
anaconda-project                   0.10.1
anyio                              2.2.0
...

Successful operation, problem solved.

Summary summary

In the recent version update of CONDA, the files under the path may be deleted_sysconfigdata_x86_64_conda_linux_GNU module related backup file, which may be relied on during the operation of some other software, which will lead to the problem that the error module cannot be found during the operation. Finally, we solved the problem by copying and backing up the file again.