Linux中Python环境变量的设置

Linux中Python环境变量的设置

Python环境变量的查看与添加

这种添加python path环境变量的方式,只在当前脚本程序范围内起作用。若要在整个Linux环境中起作用,就要添加设置Linux的PYTHONPATH变量。

jxj@ubuntu1:~/TFFRCNN$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/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', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']
>>> sys.path.insert(0, '/home/jxj/TFFRCNN')
>>> sys.path
['/home/jxj/TFFRCNN', '', '/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', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']
>>> quit()
jxj@ubuntu1:~/TFFRCNN$ 
jxj@ubuntu1:~/TFFRCNN$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/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', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']

Python环境变量的添加(修改PYTHONPATH变量)

jxj@ubuntu1:~/TFFRCNN$ export PYTHONPATH="/home/jxj/TFFRCNN:$PYTHONPATH"
jxj@ubuntu1:~/TFFRCNN$ echo $PYTHONPATH
/home/jxj/TFFRCNN:
jxj@ubuntu1:~/TFFRCNN$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/jxj/TFFRCNN', '/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', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']
>>> quit()

当终端关闭后,该环境变量也会失效。若要每次打开终端环境都能有效,将export PYTHONPATH=”/home/jxj/TFFRCNN:$PYTHONPATH” 添加至 ~/.bashrc 最后即可。

jxj@ubuntu1:~$ nl .bashrc | tail
    87  # enable programmable completion features (you don't need to enable
    88  # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
    89  # sources /etc/bash.bashrc).
    90  if ! shopt -oq posix; then
    91    if [ -f /usr/share/bash-completion/bash_completion ]; then
    92      . /usr/share/bash-completion/bash_completion
    93    elif [ -f /etc/bash_completion ]; then
    94      . /etc/bash_completion
    95    fi
    96  fi
jxj@ubuntu1:~$ 
jxj@ubuntu1:~$ vim .bashrc 
jxj@ubuntu1:~$ 
jxj@ubuntu1:~$ nl .bashrc | tail
    89  # sources /etc/bash.bashrc).
    90  if ! shopt -oq posix; then
    91    if [ -f /usr/share/bash-completion/bash_completion ]; then
    92      . /usr/share/bash-completion/bash_completion
    93    elif [ -f /etc/bash_completion ]; then
    94      . /etc/bash_completion
    95    fi
    96  fi

    97  export PYTHONPATH="/home/jxj/TFFRCNN:$PYTHONPATH"
jxj@ubuntu1:~$ source .bashrc

你可能感兴趣的:(Linux基础学习)