首页 > 解决方案 > 为什么我们需要在python中使用“as”来导入

问题描述

来自 python 文档:

 import_stmt:    "import" module ["as" name] ("," module ["as" name] )* 
          | "from" module "import" identifier ["as" name]
            ("," identifier ["as" name] )*
          | "from" module "import" "*" 
 module:         (identifier ".")* identifier

为什么我们需要为库定义别名?

标签: pythonpython-import

解决方案


有几个场合。

避免名称冲突

from bisect import bisect
from homework import bisect as my_bisect
# TODO: test if the two functions works similarly

避免长名称

import tensorflow as tf

制作表情包

import keras as tf

推荐阅读