0

I want to open an existing virtual environment in Visual Studio Code. When I try to change the interpreter address inside of the settings.json (as instructed by https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments) it doesn't work. With the virtual environment that I have, there is no python.exe file, I believe that is the issue as to why the following settings.json file doesn't work.

{
    "python.pythonPath": "D:\\GitProjects\\OrganizationApp\\venv1\\bin\\python"
}
  • https://stackoverflow.com/questions/54106071/how-to-setup-virtual-environment-for-python-in-vs-code - this might help you – Rahul Raut Aug 26 '20 at 05:47
  • @Rahul Raut I tried all of the solutions on that page and all of them either didn't work or are outdated with the new updates to vsc – Timothy Cottrell Aug 26 '20 at 05:54
  • Is there no python.exe file in Scripts folder inside the virtual environment? – S4rt-H4K Aug 26 '20 at 06:17
  • @Timothy Cottrell Could you normally use other environments in VSCode? Referecce: https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment Could you use Python in the CMD window? It is recommended that you could check the python environment variables. – Jill Cheng Aug 26 '20 at 08:40
  • @Jill Cheng I could not use python in the cmd window (after I switched the interpreter back to the original). How would I go about fixing that? I remember being able to use python in vsc when working on the project (before getting this new computer and running into all these problems) – Timothy Cottrell Aug 26 '20 at 15:34
  • @S4rt-H4K No there is no python.exe. just a python and a python3 file. The virtual environment works just fine with things like git bash. I believe it doesn't have a python.exe file because of the way I created the venv. ```python3 -m venv ./venv1``` – Timothy Cottrell Aug 26 '20 at 15:36

2 Answers2

0

First of all Try open the root folder which venv folder resides in with Visual Studio Code. (If you are on Linux you can just got the directory and open terminal and type code).

You should get a pop up to change the interpreter.

If not then Open your command palette Ctrl + Shift + P Search for Python interpreter and select the Venv one.

nvdNK
  • 3
  • 1
  • https://i.imgur.com/gbBZwgX.png this screenshot shows me trying both ways you suggested, I noticed that on the site I linked that all of the examples of virtual environments contained a python.exe file that vsc could see as an interpreter. However, my virtual environment doesn't (and I know the venv works because I used it while working on my project). I don't know enough about this kind of stuff to really know what the issue could be. @nvdNK – Timothy Cottrell Aug 26 '20 at 15:25
0

According to your description, you could refer to the following steps to check the creation and use of the virtual environment in VSCode:

Prerequisites.

  1. Check if python is available.

Enter cmd from the computer, enter the cmd window, enter python, and output the python version number to represent python is available.

enter image description here

If it is not available, please check the python installation package and check the python environment variables.

  1. The python extension was successfully installed in VSCode.

Create and use a virtual environment:

  1. Create a virtual environment in the vscode terminal. python3 -m venv ./venv1 or python -m venv ./venv1 Reference: virtual environments.

  2. Select Yes when the prompt box pops up.

    enter image description here

  3. Then the interpreter will automatically be replaced with the created virtual environment.

    enter image description here

  4. After restarting VSCode, there is no python interpreter displayed in the lower left corner. We can create or open a python file and it will automatically display the interpreter.

  5. Click on the interpreter to choose other available interpreters.

    enter image description here

  6. Use the shortcut key Ctrl+Shift+` to open a new terminal and enter the virtual environment.

    enter image description here

Update:

When I open other projects or don’t open any project in VSCode , the virtual environment created before is not displayed in the python interpreter options. The reason is that the virtual environment we created is based on the current project and it exists in this project. like this:

enter image description here

Although this virtual environment is not displayed in other projects, I can use this virtual environment by selecting the python.exe of the virtual environment ( 'enter interpreter path' '.venv' 'Scripts'). Therefore, if there is no python.exe, it is recommended to create a new virtual environment.

enter image description here

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
  • I already have a virtual environment with my project work already in it. How to I tell vs code to use that interpreter. I tried entering the path directly but it is only looking for a python.exe file (like in the images that you used in this answer) and my venv doesn't have a python.exe file. @Jill Cheng – Timothy Cottrell Aug 27 '20 at 17:47
  • @Timothy Cottrell There should be an .exe file here. Usually, after the virtual environment is successfully created, it will be displayed in the python interpreter options. No display and no exe file should be due to the unsuccessful creation of the virtual environment or the file damage. – Jill Cheng Aug 28 '20 at 02:44
  • @Timothy Cottrell Since the python project does not exist in the virtual environment, only the packages and modules required by the project are placed in the virtual environment. Therefore, it is recommended that you back up the module information in the venv1 environment and then create a new virtual environment (python3 -m venv. /venv2). (Backup: You could use the command 'pip freeze > a.txt' to import the list information of the modules in the venv1 virtual environment into a.txt, and reinstall these modules in the venv2 environment.) – Jill Cheng Aug 28 '20 at 02:44
  • @Timothy Cottrell I have added some information in my answer, you could view it as a reference. – Jill Cheng Aug 28 '20 at 04:54