激活python虚拟环境如何修改sys.path?

时间:2019-02-25 17:45:19

标签: python bash python-venv

我使用以下命令创建我的python虚拟环境

python3 -m venv venv3

要激活,我source venv3/bin/activate

venv3/bin/activate似乎并不那么复杂:

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
    # reset old environment variables
    if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
        PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
        export PYTHONHOME
        unset _OLD_VIRTUAL_PYTHONHOME
    fi

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
        hash -r
    fi

    if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
        PS1="$_OLD_VIRTUAL_PS1"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi

    unset VIRTUAL_ENV
    if [ ! "$1" = "nondestructive" ] ; then
    # Self destruct!
        unset -f deactivate
    fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/home/pi/django-test/venv3"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "$PYTHONHOME" ] ; then
    _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
    unset PYTHONHOME
fi

if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x(venv3) " != x ] ; then
    PS1="(venv3) $PS1"
    else
    if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
        # special case for Aspen magic directories
        # see http://www.zetadev.com/software/aspen/
        PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
    fi
    fi
    export PS1
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands.  Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
    hash -r
fi

我可以看到它修改了$ PATH和$ PS1,创建了一个deactivate函数,甚至备份了它修改的旧变量,以便当用户运行deactivate函数时可以恢复它们。所有这些都是有道理的。

我看不到的一件事是修改了python的sys.path。在我的系统上,这是我看到的:

虚拟环境之外的sys.path:

['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
虚拟环境中的

sys.path:

['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/home/pi/django-test/venv3/lib/python3.5/site-packages']

很显然,sys.path在某些时候以某种方式被修改。这是有道理的,因为python知道在哪里可以找到已安装的第三方python库。我认为这是虚拟环境的主要功能,但是我看不到它的设置位置。

我什么都不想做-只是好奇。

2 个答案:

答案 0 :(得分:4)

sys.pathsite.py中启动,它是使用sys.prefix的相对路径设置的,该相对路径是虚拟环境中python可执行文件的路径。

假设您使用的是virtualenv而不是-m venv,则在虚拟环境的站点目录下,通过名为no-global-site-packages.txt的标志文件来控制对系统范围的站点包的访问。

如果创建的虚拟环境没有选项--system-site-packages,则将名为no-global-site-packages.txt will be written的文件放入venv的site目录中。

在python启动期间,将执行site.py中的no-global-site-packages.txtsys.path中的check the existence,如果该标志文件不存在,系统范围的站点程序包路径将添加到{{1 }},从sys.real_prefix推论得出。 site.py  在virtualenv中创建的venv是a modified version

希望这可以回答您的问题。

答案 1 :(得分:2)

简短的答案是,激活虚拟环境不会改变"JOptionPane.showMessageDialog(null, "ERROR DE PENSION");" //1. Definicion de variables int pension; String categoria; Double Promedio; Double Descuento; categoria = txtTP.getText().toString(); System.out.println("hola" +categoria); if (categoria.equalsIgnoreCase("A")) pension = 550; else if(categoria.equalsIgnoreCase("B")) pension = 500; else if(categoria.equalsIgnoreCase("C")) pension = 460; else if (categoria.equalsIgnoreCase("D")) pension = 400; else pension = 0; JOptionPane.showMessageDialog(null, "ERROR DE PENSION"); 。 Python启动后就确定了sys.path;参见https://docs.python.org/3.7/library/sys.html#sys.path。通过调整您的sys.path环境变量,虚拟环境的作用是更改仅运行PATH时解释器实际运行的方式。