zealotds 发表于 2013-1-15 01:58:56

My Python Tips 1: chdir, load module

python, chdir, sys.path, working directory, load module

# Directory relative -----------------# current working directoryos.getcwd()# getcwd UNICODE version os.getcwu()# path separator, : for UINX and ; for Winos.pathsep# change your current working directory, useful in Interactive modeos.chdir(path)# alternative of chdir, invoke a OS shell command directlyos.system(command)# To load your module ----------------# module search path list# append your moudule path to importsys.pathsys.path.append(path)# import your module# remember that your module name should better not same to# some built-in module, otherwise it python will load the built-in# modules instead of yours# to check available built-in module: (Interactive mode)# help('modules')import <module_name># call this global to reload your modulereload(module_name)

Set a environment variable to avoid setting the search path every time:
export PYTHONPATH=$PYTHONPATH:<you-lib-path>
页: [1]
查看完整版本: My Python Tips 1: chdir, load module