0

在 Windows 10 上,我正在运行 VS Code 1.48.2 和 Python 3,它们都安装在拇指驱动器上。我添加了一个带有和的F:\Programs\VS Code\data文件夹。我的项目目录是,它有一个 Pipfile。我的项目目录有一个文件,因此将安装到本地. 我还需要什么才能让这个环境正常工作?谢谢!user-dataextensionsF:\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"
    ]
}

使用以下步骤进行设置:

ps > cd F:\MyProject
ps > F:\Programs\VS Code\code.exe .

[In vscode integrated terminal]
PS F:\MyProject> py -m venv --system-site-packages .venv
PS F:\MyProject> pipenv shell
Loading .env environment variables…
Warning: Your Pipfile requires python_version 3.8, but you are using unknown (F:\M\.venv\S\python.exe).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS F:\MyProject> ls .\.venv\Scripts\python.exe

    Directory: F:\MyProject\.venv\Scripts

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----           9/6/20   3:04 PM         532040 python.exe

PS F:\MyProject> python where
Python path configuration:
  PYTHONHOME = 'F:\MyProject\.venv\Scripts'
  PYTHONPATH = 'F:\Python\Python38\python.exe'
  program name = 'F:\Python\Python38\python.exe'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'F:\\Python\\Python38\\python.exe'
  sys.base_prefix = 'F:\\MyProject\\.venv\\Scripts'
  sys.base_exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
  sys.executable = 'F:\\MyProject\\.venv\\Scripts\\python.exe'
  sys.prefix = 'F:\\MyProject\\.venv\\Scripts'
  sys.exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
  sys.path = [
    'F:\\Python\\Python38\\python.exe',
    'F:\\Python\\Python38\\python38.zip',
    'F:\\MyProject\\.venv\\Scripts\\DLLs',
    'F:\\MyProject\\.venv\\Scripts\\lib',
    'F:\\Python\\Python38',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000020d4 (most recent call first):
<no Python frame>
PS F:\MyProject>
4

1 回答 1

0

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

  1. 在 VSCode 中打开这个项目。

根据你提供的信息,你的项目中已经有一个虚拟环境“ ”,所以打开后.venv会有一个“ ”文件夹。.venv像这样:

在此处输入图像描述

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

    在此处输入图像描述

    在此处输入图像描述

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

    在此处输入图像描述

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

    在此处输入图像描述

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

    在此处输入图像描述

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

    在此处输入图像描述

于 2020-09-10T06:48:52.890 回答