ITPub博客

首页 > 应用开发 > IT综合 > python学习第五天-模块

python学习第五天-模块

IT综合 作者:紫翼龙王夜 时间:2015-04-08 19:03:55 0 删除 编辑
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://www.itmin.cn/yunweikaifa/python_jiaoben/841.html
1 导入系统已安装的模块
    [root@localhost bin]# python
    Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type "help", "copyright", "credits" or "license" formore information.
    >>> import sys
    >>> import re
    >>> import os
    >>>
     
    2 创建模块的使用
    [root@localhost test]# cat mymodule.py
    def hello():
        print "mymodule,hello!!"
    moduleversion='1.0'
    [root@localhost test]# python
    Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
    Type "help", "copyright", "credits" or "license" formore information.
    >>> import mymodule
    >>> from mymodule import hello,moduleversion
    >>> mymodule.hello()
    mymodule,hello!!
    >>> mymodule.moduleversion
    '1.0'
     
    3 不在模块的目录下面执行那会不会成功?
    [root@localhost python]# pwd
    /opt/python
    [root@localhost python]# cd ..
    [root@localhost opt]# python
    Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type "help", "copyright", "credits" or "license" formore information.
    >>> import module
    Traceback (most recent call last):
    File "", line 1, in 
    ImportError: No module named module
    >>>
     
    如何解决???
     
    [root@localhost opt]# pwd
    /opt
    [root@localhost opt]# python
    Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type "help", "copyright", "credits" or "license" formore information.
    >>> import sys
    >>> sys.path.append('/opt/python')
    >>> import module
    This is my module
    >>>
     
     
    sys.path.append只是一次性能把自己的模块路径给添加,退出后则没有了,这样用起来也不是很方便。用更好的办法
    [root@localhost opt]# export PYTHONPATH=/opt/python/
    [root@localhost opt]# python
    Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
    [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
    Type "help", "copyright", "credits" or "license" formore information.
    >>> import module
    This is my module
    >>>

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30129545/viewspace-1549468/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-02-05

  • 博文量
    87
  • 访问量
    414055