jiaxp 发表于 2017-4-29 08:55:52

My Python Tips 1: chdir, load module

python, chdir, sys.path, working directory, load module
# Directory relative -----------------
# current working directory
os.getcwd()
# getcwd UNICODE version
os.getcwu()
# path separator, : for UINX and ; for Win
os.pathsep
# change your current working directory, useful in Interactive mode
os.chdir(path)
# alternative of chdir, invoke a OS shell command directly
os.system(command)

# To load your module ----------------
# module search path list
# append your moudule path to import
sys.path
sys.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 module
reload(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