Ubuntu下添加Python环境变量,,最近把自己在wind


最近把自己在windows上的Django项目放在的Ubuntu上,启动时提示找不到模块

原来是自己的项目用的是虚拟环境,安装的第三方库全部在虚拟环境中 AutoTestSite/venv/Lib/site-packages

在Ubuntu中查看模块搜索路径:

chen@ubuntu:~$ pythonPython 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path[‘‘, ‘/home/chen‘, ‘/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‘]

发现并没有这个路径:AutoTestSite/venv/Lib/site-packages

所以需要添加路径:

编辑文件 sudo vi .bashrc ,在最下方添加:

export PYTHONPATH="/home/**/**/venv/Lib/site-packages:$PYTHONPATH"

chen@ubuntu:~$ sudo vi .bashrc # ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples# If not running interactively, don‘t do anythingcase $- in    *i*) ;;      *) return;;esac# don‘t put duplicate lines or lines starting with space in the history.# See bash(1) for more optionsHISTCONTROL=ignoreboth# append to the history file, don‘t overwrite itshopt -s histappend# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)HISTSIZE=1000HISTFILESIZE=2000# check the window size after each command and, if necessary,# update the values of LINES and COLUMNS.shopt -s checkwinsize# If set, the pattern "**" used in a pathname expansion context will# match all files and zero or more directories and subdirectories.#shopt -s globstar# add python project autotestsite module search path assressexport PYTHONPATH="/home/guchen/python/www/for_test/AutoTestSite/venv/Lib/site-packages:$PYTHONPATH"

执行:source .bashrc

使修改生效

再次查看sys.path,已经添加了

chen@ubuntu:~$ pythonPython 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path[‘‘, ‘/home/chen/python/www/AutoTestSite/venv/Lib/site-packages‘, ‘/home/chen‘, ‘/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‘]

Ubuntu下添加Python环境变量

评论关闭