首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在闪存驱动器中使用Virtualenv和Winpython

在闪存驱动器中使用Virtualenv和Winpython
EN

Stack Overflow用户
提问于 2016-05-06 21:51:39
回答 1查看 2.2K关注 0票数 1

我刚刚下载了Winpython 3.4.4.2,我正在尝试从闪存驱动器中使用它。我也在使用git-bash。在Win7中,我将"path“和"pythonpath”环境变量设置为包含

代码语言:javascript
复制
F:\WinPython-32bit-3.4.4.2

如果我查看真实的Scripts文件夹:

代码语言:javascript
复制
/real/Scripts
$ ls
Activate.ps1      _decimal.pyd      _msi.pyd              _sqlite3.pyd     _testimportmultiple.pyd  easy_install-3.4-script.py     easy_install.exe           pip-3.4-script.py     pip.exe           python3.dll      pywintypes34.dll  tk86t.dll
_bz2.pyd          _elementtree.pyd  _multiprocessing.pyd  _ssl.pyd         _tkinter.pyd             easy_install-3.4.exe           easy_install.exe.manifest  pip-3.4.exe           pip.exe.manifest  python34.dll     select.pyd        unicodedata.pyd
_ctypes.pyd       _hashlib.pyd      _overlapped.pyd       _testbuffer.pyd  activate.bat             easy_install-3.4.exe.manifest  msvcp100.dll               pip-3.4.exe.manifest  pyexpat.pyd       pythoncom34.dll  sqlite3.dll       vcomp100.dll
_ctypes_test.pyd  _lzma.pyd         _socket.pyd           _testcapi.pyd    deactivate.bat           easy_install-script.py         msvcr100.dll               pip-script.py         python.exe        pythonw.exe      tcl86t.dll        winsound.pyd

我创建了一个名为'real‘的虚拟环境。现在,当我尝试激活virtualenv时,我得到:

代码语言:javascript
复制
/real/Scripts
$ source activate.bat                                
sh: @echo: command not found
sh: ./activate.bat: line 4: syntax error near unexpected token `('
sh: ./activate.bat: line 4: `if not defined PROMPT ('

/real/Scripts
$ source activate    
sh: activate: No such file or directory

activate.bat文件以以下内容开头:

代码语言:javascript
复制
@echo off
set "VIRTUAL_ENV=F:\envs\virtalenvs\real"
if not defined PROMPT (

    set "PROMPT=$P$G"

)

if defined _OLD_VIRTUAL_PROMPT (
    set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
)

如何激活virtualenv?

EN

回答 1

Stack Overflow用户

发布于 2016-05-07 23:20:28

基于上面的评论,我从下面的virtualenv源抓取了POSIX“激活”脚本:

代码语言:javascript
复制
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
    unset pydoc

    # 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 2>/dev/null
    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="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'F:\envs\virtalenvs\venv_name'; else echo '/F/envs/virtalenvs/venv_name'; fi;)"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$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" != x ] ; then
        PS1="$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

alias pydoc="python -m pydoc"

# 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 2>/dev/null
fi

在脚本中搜索VENV_NAME,然后将其替换为venv的名称(您可能需要仔细检查文件路径。将脚本放到venv的Scripts文件夹中,并命名为'activate‘,这样就可以了。这对我很有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37074148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档