为什么virtualenv激活脚本代码因环境而异?

2024-09-28 17:17:40 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的python项目中,我使用virtualenv作为工具来隔离我的开发环境。 2019年6月,我与我的项目一起创建了虚拟环境。 今天我需要为同一个项目创建一个新的虚拟环境,但有些东西坏了。 我运行:

$ virtualenv venv
$ source venv/bin/activate

我跑

$ python mpegg.py

得到

Traceback (most recent call last):
  File "mpegg.py", line 2, in <module>
    from parser.mpegg_file import MpeggFile
  File "/home/martina/PycharmProjects/mpeg-g/decoder/parser/mpegg_file.py", line 2, in <module>
    from .input_bit_stream import InputBitStream, read_n_bits
  File "/home/martina/PycharmProjects/mpeg-g/decoder/parser/input_bit_stream.py", line 12
    def print(self):
            ^
SyntaxError: invalid syntax

mpegg.py的第一行

import sys
from parser.mpegg_file import MpeggFile
from mpeg_post_decoder.rawreference import RawReference

对于老版的venv/我没有任何问题

我跑

$ diff venv/bin/activate venv_old/bin/activate

而且有很多不同之处

5,6d4
<     unset -f pydoc >/dev/null 2>&1
< 
8,10c6,7
<     # ! [ -z ${VAR+_} ] returns true if VAR is declared at all
<     if ! [ -z "${_OLD_VIRTUAL_PATH+_}" ] ; then
<         PATH="$_OLD_VIRTUAL_PATH"
---
>     if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
>         PATH="${_OLD_VIRTUAL_PATH:-}"
14,15c11,12
<     if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
<         PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
---
>     if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
>         PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
23,24c20,21
<     if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
<         hash -r 2>/dev/null
---
>     if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
>         hash -r
27,28c24,25
<     if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
<         PS1="$_OLD_VIRTUAL_PS1"
---
>     if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
>         PS1="${_OLD_VIRTUAL_PS1:-}"
34c31
<     if [ ! "${1-}" = "nondestructive" ] ; then
---
>     if [ ! "$1" = "nondestructive" ] ; then
51,52c48,51
< if ! [ -z "${PYTHONHOME+_}" ] ; then
<     _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
---
> # 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:-}"
56,59c55,63
< if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
<     _OLD_VIRTUAL_PS1="$PS1"
<     if [ "x" != x ] ; then
<         PS1="$PS1"
---
> if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
>     _OLD_VIRTUAL_PS1="${PS1:-}"
>     if [ "x(venv) " != x ] ; then
>   PS1="(venv) ${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"
61c65,66
<         PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
---
>         PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
>     fi
66,72d70
< # Make sure to unalias pydoc if it's already there
< alias pydoc 2>/dev/null >/dev/null && unalias pydoc
< 
< pydoc () {
<     python -m pydoc "$@"
< }
< 
76,77c74,75
< if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
<     hash -r 2>/dev/null
---
> if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
>     hash -r

为什么如此不同?它与virtualenv版本相关吗


Tags: pathpydevimportenvifvenvvirtual