简体   繁体   中英

How to set a Python Interpreter from inside a existing virtual environment in Visual Studio code

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

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.

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.

在此处输入图片说明

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.

    在此处输入图片说明

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

    在此处输入图片说明

  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.

    在此处输入图片说明

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

    在此处输入图片说明

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:

在此处输入图片说明

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM