0

On Windows 10, I'm running VS Code 1.48.2 and Python 3, both installed on a thumb drive. I added an F:\Programs\VS Code\data folder with user-data andextensions. My project directory is F:\MyProject and it has a Pipfile. My project directory has a .env file and .venv\ so that pipenv will install to the local .venv\. What else do I need to get this environment working? Thanks!

.env file:

PYTHONPATH="F:\\Python\\Python38\\python.exe"
PYTHONHOME="F:\\MyProject\\.venv\\Scripts"
PIPENV_VENV_IN_PROJECT=True

F:\MyProject\.vscode\settings.json file:

{
    "python.pythonPath": "f:\\MyProject\\.venv\\Scripts\\python.exe"
}

Although, when I used Preferences: Open Settings, it opened my F:\Programs\VS Code\data\user-data\settings.json file, though the paths look right (to me):

{
    "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"
    ]
}

Used the following steps to setup:

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>
Meghan M.
  • 129
  • 1
  • 13
  • What is the content when you type "python --version" in cmd window? When python is available, confirm that the python extension is installed in VSCode, then open the project in VSCode and select this environment in the project. Reference: https://code.visualstudio.com/docs/python/python-tutorial – Jill Cheng Sep 08 '20 at 05:59
  • Hi! I checked both the Powershell and VS Code terminal: ```PS F:\MyProject> python --version Python 3.8.5``` – Meghan M. Sep 09 '20 at 20:48
  • Right now it's in a working a state, and _I don't know why_. I want to be able to open VS Code and run a script to consistently get the environment.I thought `.env` file would help but it doesn't seem to make much difference when it comes to adding a new library, ie `pipenv install PySimpleGui`. – Meghan M. Sep 09 '20 at 21:00

1 Answers1

0

According to your description, you could refer to the following steps to use the virtual environment:

  1. Open this project in VSCode.

According to the information you provided, there is already a virtual environment ".venv" in your project, so there will be a ".venv" folder after opening. like this:

enter image description here

  1. Click "select Python Interpreter" in the lower left corner to select the ".venv" virtual environment:

    enter image description here

    enter image description here

  2. VSCode has selected the virtual environment, and then we open a new terminal console through the shortcut key "Ctrl+Shift+`", VSCode will automatically enter the current virtual environment:

    enter image description here

  3. Use pip to install the module "PySimpleGui": (pip install PySimpleGui)

    enter image description here

  4. When we run the python script, VSCode showed that the module PySimpleGui could not be found. We found "Lib\site-packages" in the ".venv" folder of the project and changed "PySimpleGUI" to "PySimpleGui":

    enter image description here

  5. The python script runs successfully in the virtual environment:

    enter image description here

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • Step 2 is where it seems to be intermittent. I pulled the thumb drive to a different machine and put it back on my main machine today. I double-checked that variables all read either ${workspacefolder} or specified the correct current drive. The "Select Python Interpreter" only shows "F:\MyProject". I can navigate to "F:\MyProject\.venv\Scripts\python.exe" but it doesn't select it or add it to the interpreter list as a choice. Thanks! – Meghan M. Sep 10 '20 at 23:07
  • @Meghan M. When you navigate to "F:\MyProject\.venv\Scripts\python.exe", is VSCode still unable to select and use this virtual environment? The virtual environment exists in the project, so when we open this project, the virtual environment will appear in the project and selector list. In addition, if it is not optional or available, it is recommended that you could try to create a new virtual environment. (python -m venv .venv02) – Jill Cheng Sep 11 '20 at 02:40
  • First, thank you for helping!! Before I saw your response, I deleted the settings.json files from "MyProject\.vscode\" & "Programs\VS Code\data\user-data\User", and also deleted "MyProject\rr.code-workspace". I restarted "F:\Programs\VS Code\code.exe rr.py". This time it showed "F:\MyProject\.venv\Scripts\python.exe" in the list of interpreters and let me select it. It successfully ran the python program. Using VS Code gui, I added the folder to rr.code-workspace. Good so far! BUT, the settings.json files all say "F:\...". Will using ${workspace} work between different machines? – Meghan M. Sep 11 '20 at 17:42
  • Also, you suggest creating .venv02 but didn't say if it makes a difference to do it in a VS Code Terminal or separate Powershell window. Does it make a difference? And should a Workspace be created before a .venv or after? – Meghan M. Sep 11 '20 at 17:46
  • @Meghan M. 1. User settings is a global setting that works every time you open a new project. Workspace settings are the settings for the current workspace project. Whether the settings will work on different machines depends on their location. Workspace settings exist in the project, while user settings usually exist on the local machine. Therefore, if they are not brought to the new machine, it will not work. Reference: [settings in VSCode](https://code.visualstudio.com/docs/getstarted/settings) – Jill Cheng Sep 12 '20 at 12:21
  • @Meghan M. 2. They are just different platforms. You could directly create a new virtual environment at the VSCode terminal. After the creation, VSCode will let you choose whether to use it or not. Because the newly created virtual environment will exist in the currently opened project, there will be a project first and then create a virtual environment. There is no necessary order of precedence between setting up project information and creating virtual environment. Reference: [virtual environment](https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments) – Jill Cheng Sep 12 '20 at 12:35
  • Think I'm going to try and get some direct info from VS Code support for this situation. Will post any follow up I get from them. Thanks! – Meghan M. Sep 13 '20 at 21:06
  • _So far_ the only working solution I've found to work regularly is to remove all `.vscode` folders and all "settings.json" files from _everywhere_ except the `\Programs\Microsoft VS Code\data\user-data\User` directory on the USB. This includes the project directory. The remaining code-workspace and `user-data\User` files should then use the setting `"python.pythonPath": ".venv\\Scripts\\python.exe"`. Using ${workspaceFolder} doesn't work. Also do NOT use the GUI to launch at all because it automatically creates a `.vscode` dir. Finally add PIPENV_VENV_IN_PROJECT=1 to your .venv startup script. – Meghan M. Sep 19 '20 at 21:39
  • Just an update that I filed this with VS Code and it's up for vote to fix. Also on the Python side: one would either have to setup a venv for each type of hardware OR write a script to make changes to the venv startup script in order to adjust the `home` setting for the drive letter change for each hardware.Wish I had better news but they're both being considered. The `PIPENV_VENV_IN_PROJECT` is definitely required in order to make sure pipenv uses the local `.venv` rather than the `C:\Users\user\.virtualenvs\`. Filed that doc request with the Pipenv folks. – Meghan M. Sep 26 '20 at 19:03