Tag Archives: Python basics

AttributeError: module ‘time‘ has no attribute ‘clock‘ [How to Solve]

AttributeError: module ‘time’ has no attribute ‘clock’
Error Messages:

# `flask_sqlalchemy` Error:
File "D:\python38-flasky\lib\site-packages\sqlalchemy\util\compat.py", line 172, in <module>
    time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'

reason:

Python 3.8 no longer supports time.clock, but it still contains this method when calling. There is a version problem.

Solution:

Use the replacement method: time.perf_Counter(), such as:

import time
if win32 or jython:
    # time_func = time.clock
    time_finc = time.perf_counter()
else:
    time_func = time.time

How to Solve RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu

In this case, the data and models are generally one in the CPU, one in the GPU, or the data used for calculation. Some have been put in the GPU, and some still exist in the CPU. Here is an idea.

First, find the error reporting line, see which variables or data are used in the calculation, and then use. Is_ CUDA this attribute to check which are on the GPU and which are on the CPU, and then put them on the CPU or GPU. for instance:

The error message indicates that there is a problem in line 46 of utils.py. Then enter the file and locate line 46

Print idx_ Range and reverse_ Mapping to check whether it is on the GPU or the CPU at the same time,

print(idx_ range.is_cuda,reverse_mapping.is_cuda)

After verification, IDX was found_ Range on CPU, reverse_ Mapping on GPU, idx_ Range on GPU (IDX)_ Range. To (device)), problem-solving

python: This application failed to stat could not find or load the Qt platform plugin “windows”

there was a problem that the graphics window could not be used when drawing with matplotlib today:

This application failed to start because it could not find or load the Qt platform plugin “windows”
in “”.

Reinstalling the application may fix this problem.

That’s the case with

.

I had no problem using it before, but suddenly today, I think it must be because I have just reinstalled some packages or library files of the system that have not been installed, or the environment is configured.

because python was installed before, then install whatever package you need, step by step install over, after reinstall the system for convenience, install anaconda, so as not to rely on the environment too much trouble. However, there was a problem: the application failed to start because it could not find or load the QT platform plug-in “Windows” .

looked up some related column problems on the Internet, and finally solved the problem.

plug-ins cannot be found or loaded, in two cases 1: no relevant software or libraries are installed. 2: Yes, but there is no associated path at call time.

I looked at Qt’s installation package:

is all there is, so the problem should be the path setting. There are many related problems on the Internet, the main one is to add it in the environment variable, the steps are as follows:

variable name: QT_QPA_PLATFORM_PLUGIN_PATH

plugins variable value: D:\Anaconda\ PKGS \qt-5.6.2-vc14h6f8c307_12\Library\plugins (my path, installation location changed, changed)

remember that it’s the qt plugins path.

if the problem is the above, then 90% of the solution is like this. At the beginning, I also added the variable value wrong, so I failed several times, but as long as you find the right path, you can solve ( after every change in the variable remember to restart the software, run again, otherwise no change ).

Python dynamically imports objects, importlib.import_ Module() uses

Background of

  • a function needs to be run by dynamically importing the corresponding configuration file according to the configuration of different projects.
solution

  • file structure
a #文件夹
	│a.py
	│__init__.py
b #文件夹
	│b.py
	│__init__.py
	├─c#文件夹
		│c.py
		│__init__.py

# c.py 中内容
args = {'a':1}

class C:
    
    def c(self):
        pass

Purpose of the

  • to a module import c.p y objects in the

  • solution

    a.py

    
    import importlib
    
    params = importlib.import_module('b.c.c') #绝对导入
    params_ = importlib.import_module('.c.c',package='b') #相对导入
    
    # 对象中取出需要的对象
    params.args #取出变量
    params.C  #取出class C
    params.C.c  #取出class C 中的c 方法
    

    import_module