2020年6月11日

command line 參數

import sys print("the number of arguments is %d" % len(sys.argv)) print(sys.argv) for i in range(len(sys.argv)): print("%d:" % i + sys.argv[i]) 執行:
$ python testarg.py a b c
the number of arguments is 4
['testarg.py', 'a', 'b', 'c']
0:testarg.py
1:a
2:b
3:c

2020年6月9日

modify module path in source code

import module 的 search path,可以用下面的 code 列出來.. import sys print(sys.path) 結果: ['', 'C:\\Users\\charles.chang\\Anaconda3\\envs\\mytest\\python38.zip', 'C:\\Users\\charles.chang\\Anaconda3\\envs\\mytest\\DLLs', 'C:\\Users\\charles.chang\\Anaconda3\\envs\\mytest\\lib', 'C:\\Users\\charles.chang\\Anaconda3\\envs\\mytest', 'C:\\Users\\charles.chang\\Anaconda3\\envs\\mytest\\lib\\site-packages'] 所以就可以修改 sys.path,變更程式 import module 的 search path..
例如 sys.path.insert(0,"C:\\mymodule") 當然,這行code 要寫在 import 之前...