我正在尝试通过 pip 在虚拟环境(Python 2.7)中安装 opencv-python 包。不幸的是,我收到一个错误:
(venv) $ pip2 install opencv-python
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting opencv-python
Using cached opencv-python-4.3.0.38.tar.gz (88.0 MB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
ERROR: Command …Run Code Online (Sandbox Code Playgroud) 我总是对这个事实感到恼火:
$ cat foo.py
def foo(flag):
if flag:
return (1,2)
else:
return None
first, second = foo(True)
first, second = foo(False)
$ python foo.py
Traceback (most recent call last):
File "foo.py", line 8, in <module>
first, second = foo(False)
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
事实是,为了正确解压而没有麻烦,我要么捕获TypeError,要么有类似的东西
values = foo(False)
if values is not None:
first, second = values
Run Code Online (Sandbox Code Playgroud)
这有点烦人.有没有一个技巧可以改善这种情况(例如,在没有foo返回(无,无)的情况下将第一个和第二个设置为无)或关于像我提出的案例的最佳设计策略的建议?*变量可能吗?
我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …Run Code Online (Sandbox Code Playgroud) 我有一个图表,上面有多个数据集。随着数据的更新,我需要不断地重新绘制这些线,每条线都分开。如何重复删除和重新建立它,最好不必每次都删除整个图形并重绘其上的所有线条?
我在codecademy项目中的指示是
定义一个名为purify的函数,它接收一个数字列表,删除列表中的所有奇数,然后返回结果.例如,purify([1,2,3])应该返回[2].不要直接修改您输入的列表; 相反,返回仅包含偶数的新列表.
我提出的代码是:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure = pure.remove(num)
return pure
Run Code Online (Sandbox Code Playgroud)
错误是:
哎呀,再试一次.您的函数在[1]上作为输入崩溃,因为您的函数抛出"'NoneType'对象不可迭代"错误.
正如我已经理解它,这意味着我的代码中的某些内容将被返回为"无".或者那里没有数据.我似乎无法找到这个简短的代码有什么问题,除非它在我的if语句中.提前致谢.
所以我在代码中进行了编辑以删除"pure = pure.remove(num)"并解决了无问题.但是,当运行代码时,它仍然无法输入[4,5,5,4]并返回[4,5,4].
新守则:
def purify(numbers):
pure = numbers
for num in pure:
if num % 2 != 0:
pure.remove(num)
return pure
Run Code Online (Sandbox Code Playgroud) 我在下面的代码中得到'NoneType'对象不是可迭代的TypeError.下面的代码是使用pyautogui滚动数字文件夹中的10个图像(名称为0到9,以图像中的#命名),当它找到一个时,报告x的值以及它找到的数字.然后按x值对字典进行排序,以读取图像中找到的数字.
问题:我还在学习Python,这个TypeError让我st脚,我怎么能纠正这个?
#! python3
import sys
import pyautogui
# locate Power
found = dict()
for digit in range(10):
positions = pyautogui.locateOnScreen('digits/{}.png'.format(digit),
region=(888, 920, 150, 40), grayscale=True)
for x, _, _, _ in positions:
found[x] = str(digit)
cols = sorted(found)
value = ''.join(found[col] for col in cols)
print(value)
Run Code Online (Sandbox Code Playgroud)
回溯错误:
Traceback (most recent call last):
File "C:\Users\test\python3.6\HC\power.py", line 10, in <module>
for x, _, _, _ in positions:
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud) python ×5
debugging ×1
function ×1
graphing ×1
list ×1
matplotlib ×1
opencv ×1
pip ×1
python-2.7 ×1
python-3.x ×1
refresh ×1
return-value ×1
typeerror ×1
virtualenv ×1