python sys模块sys.path使用方法示例

yipeiwu_com6年前Python基础

python sys模块包含了与python解释器和它的环境有关的函数,这个你可以通过dir(sys)来查看他里面的方法和成员属性

复制代码 代码如下:

import sys
print dir(sys)

result:

复制代码 代码如下:

['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']

复制代码 代码如下:

import sys
print sys.path
result:
['C:\\Documents and Settings\\username\\My Documents\\Aptana Studio 3 Workspace\\Python_Test_Project\\src', 'C:\\Documents and Settings\\username\\My Documents\\Aptana Studio 3 Workspace\\Python_Test_Project\\src', 'C:\\Python27', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode', 'C:\\WINDOWS\\system32\\python27.zip']

里面有个 sys.path属性。他是一个list.默然情况下python导入文件或者模块的话,他会先在sys.path里找模块的路径。如果没有的话,程序就会报错。
所以我们一般自己写程序的话。最好把自己的模块路径给加到当前模块扫描的路径里,eg: sys.path.append('你的模块的名称'),这样程序就不会
因为找不到模块而报错。。

相关文章

djang常用查询SQL语句的使用代码

djang常用查询SQL语句的使用代码

将django语法和sql对应一下,希望对大家有所帮助 查询单个列的值 story.object.values_list("url", flat=True) SELECT `sto...

windows10下安装TensorFlow Object Detection API的步骤

windows10下安装TensorFlow Object Detection API的步骤

安装步骤: 模型源码:https://github.com/tensorflow/models 1、下载源码后解压,修改文件夹名为models (以下步骤中涉及到路径的地方需要根据自己的...

python实现在pandas.DataFrame添加一行

实例如下所示: from pandas import * from random import * df = DataFrame(columns=('lib', 'qty1', 'q...

Python实现查找系统盘中需要找的字符

本文实例讲述了Python实现查找系统盘中需要找的字符。分享给大家供大家参考。具体如下: ''' Created on 2011-7-13 @author: 123 ''' impo...

Python 创建子进程模块subprocess详解

最近,我们老大要我写一个守护者程序,对服务器进程进行守护。如果服务器不幸挂掉了,守护者能即时的重启应用程序。上网Google了一下,发现Python有很几个模块都可以创建进程。最终我选择...