如何更改pipenv / virtualenv的位置以使用Portable VS Code并在拇指驱动器上投影?

问题描述

在Windows 10上,我正在运行VS Code 1.48.2和Python 3,它们都安装在拇指驱动器上。我添加F:\Programs\VS Code\datauser-data的{​​{1}}文件夹。我的项目目录为extensions,它有一个Pipfile。我的项目目录包含一个F:\MyProject文件.env,因此.venv\将安装到本地pipenv。为了使该环境正常工作,我还需要什么?谢谢!

.venv\文件

.env

PYTHONPATH="F:\\Python\\python38\\python.exe" PYTHONHOME="F:\\MyProject\\.venv\\Scripts" pipenv_VENV_IN_PROJECT=True 文件

F:\MyProject\.vscode\settings.json

尽管,当我使用{ "python.pythonPath": "f:\\MyProject\\.venv\\Scripts\\python.exe" } 时,它打开了我的Preferences: Open Settings文件,尽管路径对我来说似乎是正确的:

F:\Programs\VS Code\data\user-data\settings.json

使用以下步骤进行设置:

{
    "editor.suggestSelection": "first","vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue","python.languageServer": "Microsoft","python.pythonPath": "${workspacefolder}/.venv/Scripts/python.exe","python.venvPath": "${workspacefolder}/.venv","python.venvFolders": [
        ".venv","${workspacefolder}/.venv"
    ]
}

解决方法

根据您的描述,您可以参考以下步骤来使用虚拟环境:

  1. 在VSCode中打开该项目。

根据您提供的信息,项目中已经存在一个虚拟环境“ .venv”,因此打开后将存在一个“ .venv”文件夹。像这样:

enter image description here

  1. 单击左下角的“ select Python Interpreter”以选择“ .venv”虚拟环境:

    enter image description here

    enter image description here

  2. VSCode选择了虚拟环境,然后我们通过快捷键“ Ctrl + Shift +`”打开一个新的终端控制台,VSCode将自动进入当前的虚拟环境:

    enter image description here

  3. 使用pip安装模块“ PySimpleGui”:(pip install PySimpleGui

    enter image description here

  4. 当我们运行python脚本时,VSCode显示找不到模块PySimpleGui。我们在项目的“ Lib\site-packages”文件夹中找到“ .venv”,并将“ PySimpleGUI”更改为“ PySimpleGui”:

    enter image description here

  5. python脚本在虚拟环境中成功运行:

    enter image description here