模块路径:

  1. 临时增加路径
>>> import sys
>>> sys.path
>>> sys.path.append('*:\\****\\****')
复制代码
  1. 修改 PYTHONPATH 变量

打开并编辑 bashrc:

$ vim ~/.bashrc 将以下内容附加到文件末尾:

export PYTHONPATH=$PYTHONPATH:/home/wang/workspace 不要忘记重新加载 shell,方法是退出并重新启动,或者在命令行重新加载配置文件:

  1. 增加 .pth 文件

在 安装目录下/lib/site-packages 下添加一个扩展名为 .pth 的配置文件(例如:extras.pth),内容为要添加的路径:

? 1 /home/wang/workspace

模块调用

def fib(n):
	a,b=0,1
	while b < n:
		print(b,end=' ')
		a,b=b,a+b
	print()
	
def fib2(n):
	result=[]
	a,b=0,1
	while b<n:
		result.append(b)
		a,b=b,a+b
	return result

if __name__=='__main__':
	import sys
	fib(int(sys.argv[1]))


复制代码

  • 方法一:
>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> 
复制代码
  • 方法2
>>> fib=fibo.fib
>>> fib(400)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 
复制代码
  • 方法3
>>> from fibo import fib,fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 
>>> fib2(500)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
>>> 
复制代码
  • 导入模块的所以定义(除了下划线开头的)
>>> from fibo import *
>>> fib(600)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 
复制代码

  • dir函数
>>> import fibo,sys
>>> dir(fibo)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fib', 'fib2']
>>> dir(sys)
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
>>> 
复制代码