如何使用 VS Code for Windows 在 WSL (Ubuntu) 中查找和激活虚拟环境

How to find and activate a virtual environment in WSL (Ubuntu) using VS Code for Windows

我有一台 Windows 机器,我正在尝试使用 WSL,我可以使用它 Linux 来完成 Corey Schafer 关于 Django 开发的学习项目。 (一切都很好,直到我不得不使用 Gunicorn,根据我的阅读,它在 Linux 上效果最好)

我的学习项目在虚拟环境 (pipenv) 中,我的困难是如何通过 VS Code 中的 WSL 从我的 Ubuntu 发行版激活它。我找不到我的虚拟环境的路径。我试图将它添加到我的 .profile 上的 PATH 变量中,但它仍然没有 'find' 它。我尝试使用 source <path to activate file> 手动激活它。它 'activates' 但经检查它不是 运行 虚拟环境中的 python 可执行文件。在 VS Code 中,系统提示我 select 一个 python 解释器,并且我的虚拟环境中的 python 可执行文件再次不在选项中。 Windows和Ubuntu都是运行python3.7.4.

Windows VS 代码终端(运行 正确的 python 可执行文件)

(Corey Schafer - Django Framework) PS C:\Users\this_user\OneDrive - CUT\Learning\Python\Corey 
Schafer - Django Framework> python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\Users\this_user\.virtualenvs\Corey_Schafer_-_Django_Framework-jrC3nBNH\Scripts\python.exe'

VS 代码终端上的 WSL (Ubuntu)(不是正确的 python 可执行文件。还显示了我的 PATH 变量,其中添加了虚拟环境的路径)

    (Corey Schafer - Django Framework) this_user_name@this_PC:/mnt/c/Users/this_user/.virtualenvs/Corey_Schafer_-_Django_Framework-jrC3nBNH/Scripts$ python
    Python 3.7.4 (default, Dec 27 2019, 13:49:49)
    [GCC 7.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.executable
    '/usr/local/bin/python3.7'
    (Corey Schafer - Django Framework) this_user_name@this_PC:/mnt/c/Users/this_user/.virtualenvs/Corey_Schafer_-_Django_Framework-jrC3nBNH/Scripts$ echo $PATH
    /C/Users/this_user/.virtualenvs/Corey_Schafer_-_Django_Framework-rC3nBNH/Scripts:
    /mnt/c/Users/this_user/.virtualenvs:/home/this_user_name/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
    /sbin:/bin:/usr/games:/usr/local/games:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:
    /mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/PostgreSQL/12/bin:/mnt/c/Users/this_user/.virtualenvs:
    /mnt/c/Users/this_user/.virtualenvs/Corey_Schafer_-_Django_Framework-rC3nBNH/Scripts:
    /mnt/c/Users/this_user/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/this_user/AppData/Local/GitHubDesktop/bin:
    /mnt/c/Users/this_user/AppData/Local/Programs/Python/Python37-32:/mnt/c/Users/this_user/AppData/Local/Programs/Python/Python37-32/Scripts:
    /mnt/c/Users/this_user/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files/heroku/bin:/snap/bin:
    /mnt/c/Users/this_user/.virtualenvs/Corey_Schafer_-_Django_Framework-jrC3nBNH/Scripts$
我的工作区文件夹的

Settings.json 文件(我知道 python 路径是 Windows 目录,但不确定要放什么,它可以在 Windows 和 Linux。试图从 Linux 目录中输入正确的路径,但仍然没有成功)

    {
        "python.pythonPath": "C:\Users\this_user\.virtualenvs\Corey_Schafer_-_Django_Framework-jrC3nBNH\Scripts\python.exe",
        "python.autoComplete.extraPaths": [
            "./django_project"
        ],
    }

我的 .profile 文件

    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    # see /usr/share/doc/bash/examples/startup-files for examples.
    # the files are located in the bash-doc package.

    # the default umask is set in /etc/profile; for setting the umask
    # for ssh logins, install and configure the libpam-umask package.
    #umask 022

    # if running bash
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
        fi
    fi

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/.local/bin" ] ; then
        PATH="$HOME/.local/bin:$PATH"
    fi


    #Adding path to virtual enviroments
    PATH="/mnt/c/Users/this_user/.virtualenvs:$PATH"
    export PATH

从下图中,我的虚拟环境中的解释器不在要 selected 的选项中(在同一个 .virtualenv 文件夹中还有其他解释器,我在这里看不到,但我可以在Windows)。我还收到一条警告,指出已识别 pipfile 但未找到 pipenv 激活路径,这似乎又是路径问题(“”是因为我试图将路径放入 'path to pipenv' 设置那时我把它留空了。)

我是 Linux 的新手,对 VS Code 比较陌生。感谢提供的任何帮助。

这个问题是因为我 运行 Ubuntu 上 Python 的 64 位版本和 Windows 上的 32 位版本没有意识到(可以注意到在我上面的问题中)。通过在 Ubuntu 上创建虚拟环境 (venv) 并通过 pipenv run pip freeze > requirement.txt 生成的 requirements.txt 安装与我在 Windows 中的虚拟环境相同的包来解决此问题。

当我在 Windows 和 WSL Ubuntu 之间切换时,由于 Windows 和 WSL Ubuntu(据我所知),由于 [= =16=] 程序。 很高兴我从中学到了一些东西!