dive into python:模块的导入和搜索文件路径的配置

1、Python中导入模块:import sys;相当于Java中的导入包、类。

  比如,我们导入sys模块,使用:import sys;

2、Python中调用函数的时候,会从默认配置的库文件夹中(sys.path)搜索你所调用的模块。这个库文件的目录配置,如下:

  sys.path用来配置Python模块搜索路径。它是一个List(列表),打印默认sys.path的内容:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys;
>>> print(sys.path);
['', 'D:\SeleniumTestCase', 'C:\Python33\Lib\idlelib', 'C:\Python33\lib\site-packages\setuptools-5.5-py3.3.egg', 'C:\Python33\lib\site-packages\pip-1.5.6-py3.3.egg', 'C:\Windows\system32\python33.zip', 'C:\Python33\DLLs', 'C:\Python33\lib', 'C:\Python33', 'C:\Python33\lib\site-packages']
>>>

其中'D:\SeleniumTestCase'是自己使用sys.path.apend('D:\SeleniumTestCase');命令添加进去的。

通过添加自定义目录,以后你导入模块的时候,Python也会到你添加的下去找模块源码。

原文地址:https://www.cnblogs.com/MrCandy/p/3980462.html