How do I change pipenv/virtualenv location to work with Portable VS Code and project on thumb drive?

3.5k views Asked by At

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>
1

There are 1 answers

9
Jill Cheng On

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