IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Python的sys.path妙用

    caimouse发表于 2017-03-24 17:39:15
    love 0

    在Python里,经常遇到这样一种情况,自己编写的一些模块,想比较清晰地管理不同的模块,所以在当前目录下面建立了不同的子目录,如下图:


    在子目录mod里包含着要使用的py文件,如果想要使用它,可以按模块的方式来导入,但是有一种更简单的方法,就是让python搜索到mod目录,即可以使用了。


    比如要使用上面的文件test2.py,那么就可以按下面的代码来编写:

    #python 3.5.3  
    #2017-03-24 蔡军生  http://blog.csdn.net/caimouse    
    #
    
    import sys
    sys.path.append("mod/")
    import test2
    
    print(test2.test)

    输出如下:

    ========================= RESTART: D:\demo\test1.py =========================
    test2
    >>>

    test2.py的内容如下:

    test = 'test2'

    通过这种方式,可以比较快捷地使用,而不用建立__init__.py文件,并且还可以不管子目录里的依赖问题。

    当然也可以使用安装的方式,把这个模块安装python库目录里,但是那样要制作安装程序,又多了一道工序。

    1. TensorFlow API攻略

    http://edu.csdn.net/course/detail/4495
    2. TensorFlow入门基本教程
    http://edu.csdn.net/course/detail/4369

    3. C++标准模板库从入门到精通 

    http://edu.csdn.net/course/detail/3324

    4.跟老菜鸟学C++

    http://edu.csdn.net/course/detail/2901

    5. 跟老菜鸟学python

    http://edu.csdn.net/course/detail/2592

    6. 在VC2015里学会使用tinyxml库

    http://edu.csdn.net/course/detail/2590

    7. 在Windows下SVN的版本管理与实战 

     http://edu.csdn.net/course/detail/2579

    8.Visual Studio 2015开发C++程序的基本使用 

    http://edu.csdn.net/course/detail/2570

    9.在VC2015里使用protobuf协议

    http://edu.csdn.net/course/detail/2582

    10.在VC2015里学会使用MySQL数据库

    http://edu.csdn.net/course/detail/2672






沪ICP备19023445号-2号
友情链接