How to setup virtual environment for Python in VS Code?

后端 未结 12 1856
灰色年华
灰色年华 2020-11-29 16:29

In my project folder I created venv folder.

python -m venv venv

When I in VS Code run command select python interpreter my ven

相关标签:
12条回答
  • 2020-11-29 16:45

    This is an adding to @Sam answer that though is correct is missing the fact that anytime you open a folder in visual studio code, it create a .vscode folder, but those can be multiple, created any time you eventually open a directory. The .vscode folder has JSON objects that content properties such "setting.json", in which one declare the Interpreter to use at that ".vscode" level( refer to this for more clarifications What is a 'workspace' in VS Code?).

    {
       {
         "python.pythonPath": "VirtualEnPath/bin/python3.6"
       }
    }
    

    So potentially you could open VS code at another level in the virtual Env, it create another .vscode folder that assume as Python directory those of the global machine and so having such error, and has I experienced has nothing to do if the Virtual Env is activated or not.

    This indeed what happened to me, I have indeed a DjangoRESTAPI_GEN folder in which I initially opened the IDE and it did recognize the Virtual Env Python path, the a few days after I opened it at the level where git is, so it did created another .vscode, that picked the global Python Interpreter, causing my lint in the Virtual Environment not been used, and the virtual env interpreter not even showed in "select python interpreter". But as wrote opening the IDE at the level where the .vscode that has the settings.json with correct path, it does.

    Once you set the correct path in the setting.json and select the virtual env interpreter, then VS Code will automatically activate the VE in its terminal

    0 讨论(0)
  • 2020-11-29 16:45

    Have you activated your environment? Also you could try this: vscode select venv

    0 讨论(0)
  • 2020-11-29 16:50

    There is a VSCode extension called "Python Auto Venv" that automatically detects and uses your virtual environment if there is one.

    0 讨论(0)
  • 2020-11-29 16:55

    Many have mentioned the python.pythonPath method.

    Another way is adding a envFile in the launch.json like this:

        {
            "name": "Run",
            "etc": "etc",
            "envFile": "${workspaceFolder}/venv"
        }
    
    0 讨论(0)
  • 2020-11-29 16:56

    With a newer VS Code version it's quite simple.

    Open VS Code in your project's folder.

    Then open Python Terminal (Ctrl-Shift-P: Python: Create Terminal)

    In the terminal:

    python -m venv .venv
    

    you'll then see the following dialog:

    click Yes

    Then Python: Select Interpreter (via Ctrl-Shift-P)

    and select the option (in my case towards the bottom)

    Python 3.7 (venv) ./venv/Scripts/python.exe

    If you see

    Activate.ps1 is not digitally signed. You cannot run this script on the current system.

    you'll need to do the following: https://stackoverflow.com/a/18713789/2705777

    For more information see: https://code.visualstudio.com/docs/python/environments#_global-virtual-and-conda-environments

    0 讨论(0)
  • 2020-11-29 16:57

    The question is how to create a new virtual environment in VSCode, that is why telling the following Anaconda solution might not the needed answer to the question. It is just relevant for Anaconda users.

    Just create a venv using conda, see here. Afterwards open VSCode and left-click on the VSCode interpreter shown in VSCode at the bottom left:

    Choose a virtual environment that pops up in a dropdown of the settings window, and you are done. Mind the answer of @RamiMa.

    0 讨论(0)
提交回复
热议问题