use*_*510 5 python virtualenv windows-subsystem-for-linux
在我的 Windows 系统上,我已使用 Windows 命令提示符成功安装了虚拟环境(Python 版本为 3.9)
python -m venv C:\my_path\my_venv
始终使用 Windows 命令提示符,我可以通过以下方式激活创建的 venv
C:\my_path\my_venv\Scripts\activate.bat
我确信 venv 已正确激活,因为:
(my_venv)python) 激活 python 并运行以下命令:import sys; sys.path我可以在路径列表中看到所需的路径[..., 'C:\\my_path\\my_venv\\lib\\site-packages\\win32\\lib', ...]总而言之,到目前为止一切都很好。
我还有 WSL2 (Ubuntu),我想使用 Ubuntu 终端激活相同的 venv。如果我从 Ubuntu 终端激活 venv
source /mnt/c/my_path/my_venv/Scripts/activate
它似乎可以工作,因为命令行前面是(my_venv),但是当我运行 python ( python3command) 然后运行import sys; sys.path我看到系统的目标是基本 Ubuntu python 安装(版本 3.8),而不是 venv 安装:
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
venv 并未真正激活。有解决问题的建议吗?
如果有帮助,我会添加一些信息。
如果我尝试直接使用 Ubuntu 终端创建 venv
python3 -m venv /mnt/c/my_path/my_venv_unix
并通过 Ubuntu 终端 ( source /mnt/c/my_path/my_venv_unix/bin/activate) 激活它,一切正常,但这不是我想要的:我想使用 WSL 激活使用 Windows 命令提示符创建的虚拟环境,因为在我的计算机上我创建了很多 venv对于 Windows,我不想复制它们。
按照脚本 C:\my_path\my_venv\Scripts\activate (/mnt/c/my_path/my_venv/Scripts/activate 使用 wsl 文件夹命名)(我必须将 EOL 从 Windows 更改为 Ubuntu,否则该命令将无法source /mnt/c/my_path/my_venv/Scripts/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 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="C:\my_path\my_venv"
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:-}"
PS1="(.venv_ml_dl_gen_purpose) ${PS1:-}"
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 2> /dev/null
fi
Run Code Online (Sandbox Code Playgroud)
最后,这里还有脚本 /mnt/c/my_path/my_venv_unix/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="/mnt/c/my_path/my_venv_unix"
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(venv_unix) " != x ] ; then
PS1="(venv_unix) ${PS1:-}"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see https://aspen.io/
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
Run Code Online (Sandbox Code Playgroud)
感谢任何愿意回答的人!
简短回答: 强烈建议在 WSL 中使用 Linux 版本的 Python 和工具。您会在 Stack Overflow 上找到许多与此相关的帖子,但您的问题足够不同(关于 venv),因此它值得拥有自己的答案。
更多详情:
这个问题也值得一读。在这种情况下,问题是围绕双引导系统以及是否可以在 Windows 和 Linux 之间共享相同的 venv。
我知道WSL 上的情况似乎可能会更好,您可以.exe在 Linux 下运行 Windows 可编辑文件,但实际上并不适合这种特殊情况。
您已经解决了第一个问题,即行结尾的差异,但您面临的下一个问题是目录格式的差异。获取资源后activate,执行操作echo $PATH,您将看到Windows样式C:\path\to\the\venv路径已添加到您的PATH. 对于 WSL,这需要是/mnt/c/path/to/the/venv.
那是行不通的。
一旦你修复了这个问题(再次通过编辑activate),你仍然在尝试运行python3. venv 可执行文件实际上是python.exe. 在 WSL 下,您必须指定扩展名。
所以如果你:
activate从 Windows 格式更改为 WSL2 格式python.exe可执行文件那么你至少可以启动 Windows Python 版本。您import sys; sys.path将显示 Windows 路径。
也就是说,您几乎肯定会遇到其他问题,而这些问题不值得这样做。例如,如果脚本假设python或python3,甚至pip; 那么这些将会失败,因为它需要调用,例如pip.exe.
行结尾和本机代码也将是一个问题。
由于这些原因(可能还有更多原因),强烈建议在 WSL 中使用 Linux 版本的 Python。
| 归档时间: |
|
| 查看次数: |
5074 次 |
| 最近记录: |