Python sys.path.append{}
sys - System-specific parameters and functions - 系统特定的参数和函数
https://docs.python.org/3/library/sys.html
1. sys.path
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH
, plus an installation-dependent default.
一个字符串列表,指定模块的搜索路径。从环境变量 PYTHONPATH
初始化,加上依赖于安装的默认值。
As initialized upon program startup, the first item of this list, path[0]
, is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0]
is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH
.
在程序启动时初始化,此列表的第一项 path[0]
是包含用于调用 Python 解释器的脚本的目录。如果脚本目录不可用 (例如,如果以交互方式调用解释器或者从标准输入读取脚本),则 path[0]
为空字符串,它指示 Python 首先搜索当前目录中的模块。请注意,在作为 PYTHONPATH
结果插入的条目之前插入了脚本目录。
A program is free to modify this list for its own purposes. Only strings and bytes should be added to sys.path
; all other data types are ignored during import.
程序可以自行修改此列表。只应将字符串和字节添加到 sys.path
,导入期间将忽略所有其他数据类型。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
print(sys.path)
if __name__ == '__main__':
print("Waiting for all processes done...")
print("All processes done.")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0']
Waiting for all processes done...
All processes done.
Process finished with exit code 0
sys.path
返回的是一个列表,当我们要添加自己的搜索目录时,可以通过列表的 append()
方法。
sys.path.append()
方法可以临时添加搜索路径,这种方法导入的路径会在 Python 程序退出后失效。
2. sys.path.append(os.getcwd())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import os
sys.path.append(os.getcwd())
print(os.getcwd())
print(sys.path)
if __name__ == '__main__':
print("Waiting for all processes done...")
print("All processes done.")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow']
Waiting for all processes done...
All processes done.
Process finished with exit code 0
3. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import os
print("__file__:", __file__)
print("os.path.abspath(__file__):", os.path.abspath(__file__))
print("os.path.dirname(os.path.abspath(__file__)):", os.path.dirname(os.path.abspath(__file__)))
print(sys.path)
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/')
print(sys.path)
print(sys.path)
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
print(sys.path)
if __name__ == '__main__':
print("Waiting for all processes done...")
print("All processes done.")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
__file__: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
os.path.abspath(__file__): /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
os.path.dirname(os.path.abspath(__file__)): /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0']
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/']
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/']
['/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/strong/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/', '/home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/..']
Waiting for all processes done...
All processes done.
Process finished with exit code 0
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/