pythonvisual-studio-codepython-2.x

Create virtual environment with python 2.7.18 from VScode


I want to create a virtual environment in VS Code with python version 2.X. If I remember correctly, I was able to create an environment for Python 2 two months ago, but I can't do so anymore.

I've read that the problem is that the new versions of the Python extension for VS Code no longer allow creating virtual environments for python 2.

I have pyenv installed on windows with different versions of python, including 2.7.18 and some versions 3.x.x. Python 3 works fine, creating a virtual environment using Python 2 throws the following error:

2024-08-17 18:40:37.630 [info] Selected interpreter C:\Users\Alienware\.pyenv\pyenv-win\versions\2.7.18\python.exe for creating virtual environment.
2024-08-17 18:40:37.746 [info] Running Env creation script:  [
  'C:\\Users\\Alienware\\.pyenv\\pyenv-win\\versions\\2.7.18\\python.exe',
  'c:\\Users\\Alienware\\.vscode\\extensions\\ms-python.python-2024.13.2024081501-win32-x64\\python_files\\create_venv.py',
  '--git-ignore'
]
2024-08-17 18:40:37.746 [info] > ~\.pyenv\pyenv-win\versions\2.7.18\python.exe ~\.vscode\extensions\ms-python.python-2024.13.2024081501-win32-x64\python_files\create_venv.py --git-ignore
2024-08-17 18:40:37.746 [info] cwd: .
2024-08-17 18:40:37.847 [info]   File "c:\Users\Alienware\.vscode\extensions\ms-python.python-2024.13.2024081501-win32-x64\python_files\create_venv.py", line 23
    def parse_args(argv: Sequence[str]) -> argparse.Namespace:
2024-08-17 18:40:37.847 [info]            ^
SyntaxError: invalid syntax
2024-08-17 18:40:37.849 [error] Error while running venv creation script:  undefined
2024-08-17 18:40:37.850 [error] Failed to create virtual environment with exitCode: 1

I'm currently following these steps:

  1. Install pyenv for windows following this guide: https://github.com/pyenv-win/pyenv-win#quick-start

  2. Install this version of python:

Python versions installed

  1. Set python 2.7.18 as global interpreter with "pyenv global 2.7.18"

  2. Download and installing vscode with python extension v2024.13.2024081501 (pre-release)

  3. Create a new workspace in vscode and create a new venv with CTRL + SHIFT + P:

VENV python 2.7.18

  1. Error (interpreter already selected):

ERROR AND SELECTED INTERPRETER

  1. Console output:

Output

Is it possible to create venv from vscode for 2.x versions?


Solution

  • the venv was introduced from Python 3.3, so it can't be created directly with python 2.7.18. If you want to create a virtual environment using Python 2.7.18, you can use the virtualenv tool. To confirm that python 2.7.18 on the system has been added to the PATH environment variable. In vscode, first use the pip install virtualenv command to install the tool. Then create the virtual environment with virtualenv --python=python2.7.18 myvenv.