【Python】报错系列1

print函数没有括号

报错

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

python3输出没有加括号
分析
print函数加与不加括号,主要是在Python2和Python3中的差异,print函数在Python2中是不需要加括号的,在Python3中,需要加括号。

主要体现在以下几个方面:

  1. Python3中print是一个内置函数,有多个参数,而Python2中print是一个语法结构;
  2. Python2打印时可以不加括号:print ‘hello world’, Python3则需要加括号 print(“hello world”)
  3. Python2中,input要求输入的字符串必须要加引号,为避免读取非字符串类型发生的一些行为,不得不使用raw_input()代替input()

还有一种特殊情况

from __future__ import print_function

在开头加上这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用。python2.X中print不需要括号,而在python3.X中则需要。

方法
一个一个加括号

tensorflow缺少接口1:contrib

报错

AttributeError: module 'tensorflow' has no attribute 'contrib'

分析
使用tensorflow2.x版本的时候,使用调用tensorflow1.x函数的代码时,常常会出现module ‘tensorflow’ has no attribute ‘contrib’这样的问题,这是由于tensorflow2.x废弃了很多tensorflow1.x API接口
方法
因为tensorflow2.x版本已经没有contrib库
Tf-slim 有一个独立于 tensorflow 的镜像可以以 tf.compat.v1 兼容模式下使用,安装该包即可
TF-Slim是一个轻量级的库,用于在TensorFlow中定义,训练和评估复杂的模型。tf-slim的组件可以与本机tensorflow以及其他框架自由混合。
在cmd中下载

C:\Users\wyh>pip install --upgrade tf_slim
Collecting tf_slim
  Downloading tf_slim-1.1.0-py2.py3-none-any.whl (352 kB)
     ---------------------------------------- 352.1/352.1 kB 39.4 kB/s eta 0:00:00
Collecting absl-py>=0.2.2 (from tf_slim)
  Downloading absl_py-2.0.0-py3-none-any.whl.metadata (2.3 kB)
Downloading absl_py-2.0.0-py3-none-any.whl (130 kB)
   ---------------------------------------- 130.2/130.2 kB 31.0 kB/s eta 0:00:00
Installing collected packages: absl-py, tf_slim
Successfully installed absl-py-2.0.0 tf_slim-1.1.0

记得下载到虚拟环境中。
把源代码进行修改

#import tensorflow.contrib.slim as slim
import tf_slim as slim
#slim = tf.contrib.slim
slim = slim

#from tensorflow.contrib import layers
from tf_slim import layers

#tf.contrib.layers.xavier_initializer()
tf.keras.initializers.glorot_normal()

tensorflow缺少接口2: ConfigProto

报错

AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

分析
接口被废弃
方法

# gpu_config = tf.ConfigProto()
gpu_config = tf.compat.v1.ConfigProto()

tensorflow缺少接口3: placeholder

报错

AttributeError: module 'tensorflow' has no attribute 'placeholder'

分析
接口被废弃
方法
导入时改变

# import tensorflow as tf
# import tensorflow.compat.v1 as tf
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()

tensorflow缺少接口4: get_variable

报错

AttributeError: module 'tensorflow' has no attribute 'get_variable'

分析
接口被废弃
方法

#tf.get_variable
tf.compat.v1.get_variable

引用自建库出错

报错

NameError: name 'utils' is not defined

分析
由于引用的文件在一级文件夹中,而被引用的文件在一级下面的二级文件夹中,所以需要加上前缀作为路径
方法

#import utils as ut
import helper.utils as ut

from future import *

该语句的作用就是将新版本的特性引进当前版本中,也就是说我们可以在当前版本使用新版本的一些特性。

from future import absolute_import

绝对引入,意思为可以用import string来引入系统的标准string.py, 而用from pkg import string来引入当前目录下的string.py了,可以区分当前目录下包与系统标准库。

from future import print_function

使用后Print即使在2.x版本中也需要加括号使用。

# python 2.x
from __future__ import print_function
print("Hello World")

from future import division

导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/“操作符执行的是截断除法(Truncating Division),当我们导入精确除法之后,”/"执行的是精确除法。
例子为

# python 2.x
5/2
>>> 2
from __future__ import division
5/2
>>> 2.5

from future import with_statement

# python 2.x
try:
    with open('test.txt', 'w') as f:
    f.write('Hello World')
finally:
    f.close()
 
# 用with替代上述异常检测代码:
from __future__ import with_statement
with open('test.txt', 'w') as f:
    f.write('Hi there!')

因为 python3 中取消了 range 函数,而把 xrange 函数重命名为 range,所以现在直接用 range 函数即可。

因此,在python3版本中,我们只需要直接用range 函数就可以了!而且从某种意义上来说,xrange() 函数用法与 range() 完全相同。如下:

torch库加载超时

报错

Calling torch.linalg.lu_factor on a CPU tensor requires compiling PyTorch with LAPACK. Please use PyTorch built with LAPACK support.  

方法

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 --timeout=1000

torch官方张量类型

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值