【问题标题】:How to find and activate a virtual environment in WSL (Ubuntu) using VS Code for Windows如何使用 VS Code for Windows 在 WSL (Ubuntu) 中查找和激活虚拟环境
【发布时间】:2020-04-18 23:45:26
【问题描述】:

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

我的学习项目是在虚拟环境 (pipenv) 中,我的困难是如何通过 VS Code 中的 WSL 从我的 Ubuntu 发行版激活它。我找不到虚拟环境的路径。我试图将它添加到我的 .profile 上的 PATH 变量中,但它仍然没有“找到”它。我尝试使用source <path to activate file> 手动激活它。它“激活”但在检查时它没有从虚拟环境运行 python 可执行文件。在 VS Code 中,系统提示我选择一个 python 解释器,并且我的虚拟环境中的 python 可执行文件再次不在选项中。 Windows 和 Ubuntu 都在运行 python 3.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 Code 终端上的 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 文件(我知道 pythonpath 是用于 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

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

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

【问题讨论】:

    标签: python-3.x linux visual-studio-code windows-subsystem-for-linux vscode-remote


    【解决方案1】:

    问题是因为我在 Ubuntu 上运行 64 位版本的 Python 而在 Windows 上运行 32 位版本却不知道(可以在我上面的问题中指出)。通过在 Ubuntu 上创建虚拟环境 (venv) 并通过 pipenv run pip freeze > requirement.txt 生成的 requirements.txt 在 Windows 中安装与我的虚拟环境相同的软件包来解决此问题。

    由于 python.exe 程序的体系结构不同,当我在 Windows 和 WSL Ubuntu 之间切换(据我所见)时,这两个环境不会相互检测到,因此效果很好。 很高兴我从中学到了一些东西!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2023-03-26
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      相关资源
      最近更新 更多