码迷,mamicode.com
首页 > 编程语言 > 详细

python-模块

时间:2016-07-16 08:55:25      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

1.导入模块

import os
import os.path
from os import path
from os.path import isdir, isfile

2.动态导入模块

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

上述代码先尝试从cStringIO导入,如果失败了(比如cStringIO没有被安装),再尝试从StringIO导入。这样,如果cStringIO模块存在,则我们将获得更快的运行速度,如果cStringIO不存在,则顶多代码运行速度会变慢,但不会影响代码的正常执行。

try 的作用是捕获错误,并在捕获到指定错误时执行 except 语句。

3.使用__future__

Python的新版本会引入新的功能,但是,实际上这些功能在上一个老版本中就已经存在了。要“试用”某一新的特性,就可以通过导入__future__模块的某些功能来实现。

使用from __future__ import unicode_literals将把Python 3.x的unicode规则带入Python 2.7中。

参考代码:

from __future__ import unicode_literals
s = ‘am I an unicode?‘
print isinstance(s, unicode)

4.安装第三方模块

 -easy_install

-pip(内置到2.7.9)

pip install web.py

python-模块

标签:

原文地址:http://www.cnblogs.com/Nyan-Workflow-FC/p/5674367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!