from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Python 3.x引入了一些与Python 2不兼容的关键字和特性。在Python 2中,可以通过内置的__future__模块导入这些新内容。如果你希望在Python 2环境下写的代码也可以在Python 3.x中运行,那么建议使用__future__模块
1.1 from __future__ import absolute_import:
相对与绝对导入
1.2 from __future__ import division
精确除法
1.3 from __future__ import print_function
print用法
from absl import app
from absl import flags
from absl import logging
程序中如何使用absl输入参数
import sys
from absl import app
from absl import flags
from absl import logging
#设置参数,第一个是参数名称,第二个是参数默认值,无默认值可取None,第三个是参数解释
flags.DEFINE_string('str_1', 'hello',
'Input a string.')
flags.DEFINE_string('str_2', 'world',
'Input a string.')
flags.DEFINE_integer('num_1', 0,
'Input a integer.')
flags.DEFINE_integer('num_2', 0,
'Input a integer.')
FLAGS = flags.FLAGS
def main(argv=()):
del argv
#打印时间,以及Python版本号
version = sys.version_info
logging.info('Running under Python {0[0]}.{0[1]}.{0[2]}'.format(version))
str3 = FLAGS.str_1 + FLAGS.str_2 #计算输入两个字符串的和-拼接字符串
print(str3)
c = FLAGS.num_1 * FLAGS.num_2 #计算输入两个整数的积
print(c)
# 如果当前是从其它模块调用的该模块程序,则不会运行main函数!
# 而如果就是直接运行的该模块程序,则会运行main函数。
if __name__ == '__main__':
flags.mark_flag_as_required('str_1')
flags.mark_flag_as_required('str_2')
flags.mark_flag_as_required('num_1')
flags.mark_flag_as_required('num_2')
# 执行程序中main函数,并解析命令行参数!
app.run(main)
打开终端或者Windows的命令提示符,输入python 文件名 -help就可以查看文件参数信息,例如:
输入参数方式:
但是一直都有提示很烦:
F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --str_1 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
'command line!' % flag_name)
F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --str_2 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
'command line!' % flag_name)
F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --num_1 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
'command line!' % flag_name)
F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --num_2 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
'command line!' % flag_name)
还不知道如何消除这些警告提示,望知道的大神告知一下,不胜感激