Python的sys.path妙用

来源:互联网 发布:鞍山北国知春房价 编辑:程序博客网 时间:2024/06/05 15:52

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


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


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

#python 3.5.3  #2017-03-24 蔡军生  http://blog.csdn.net/caimouse    #import syssys.path.append("mod/")import test2print(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




0 0
原创粉丝点击