简体   繁体   English

在Visual Studio代码中设置python virtualEnv的错误

[英]Mistake in setup python virtualEnv in visual-studio-code

Trying to setup Visual-Studio-Code and running into issues configuring virtualEnv properly. 尝试设置Visual Studio代码,并遇到配置virtualEnv的问题。 Read everything i could find online and tried different solutions: none worked so far. 阅读我可以在网上找到的所有内容,并尝试使用不同的解决方案:到目前为止,没有一种方法有效。 MAYBE i am just not getting the documentation and everyone else gets it. 也许我只是没有得到文档,其他人都得到了。

  1. Proof that the virtualEnv as well as the package oauth2client exists. 证明virtualEnv以及软件包oauth2client存在。

在此处输入图片说明

  1. Path of the virtualEnv virtualEnv的路径

在此处输入图片说明

  1. Setup in Visual Studio Code (yes i restarted after saving the settings). 在Visual Studio Code中进行安装(是的,在保存设置后我重新启动了)。

在此处输入图片说明

  1. oauth2client can't be found. oauth2client As you can see in the corner the Python interpreter was rightfully found. 正如您在拐角处看到的那样,正确找到了Python解释器。

在此处输入图片说明

I hope that one of you guys can spot my mistake. 我希望你们中的一个能发现我的错误。

Update: 更新:

So thanks to mattmc3 i figured it out. 因此,感谢mattmc3,我弄清楚了。 Was using the "Code Runner" Plugin for running the python code. 正在使用“代码运行器”插件运行python代码。 I missed setting up the right Interpreter for this plugin - used still the default one. 我错过了为此插件设置正确的解释器的机会-仍然使用默认解释器。

在此处输入图片说明

Your final screenshot indicates that you are using your venv properly, which you named "py3". 最后的屏幕快照表明您正在正确使用venv ,并将其命名为“ py3”。 There are lots of ways to launch your Python script from VSCode: 有很多方法可以从VSCode启动Python脚本:

  • Right click the file in VSCode's file Explorer and select "Run Python File in Terminal" 右键单击VSCode的文件资源管理器中的文件,然后选择“在终端中运行Python文件”
  • Click the debugger menu and set up a launch.json file to ensure you are using your venv. 单击调试器菜单并设置launch.json文件,以确保您正在使用venv。 This method has the added advantage of allowing you to step through your code. 此方法的另一个优点是允许您单步执行代码。
  • Set up a tasks.json file to create a build task using your preferred venv. 设置一个task.json文件以使用您喜欢的venv创建一个构建任务。

Based on the back-and-forth comments we exchanged, it appears that you did not launch using any of these standard methods, which could explain why your venv is not being picked up. 根据我们交换的来回评论,您似乎没有使用这些标准方法中的任何一种来启动,这可以解释为什么您的静脉未被拾取。

Here is the stepbystep how to create the Virtual environment in Visual Studio Code folder: I used Powershell (Administrator mode): 这是如何在Visual Studio Code文件夹中创建虚拟环境的分步说明:我使用了Powershell(管理员模式):
1. I create a VSCode folder - "D:\\Code_Python_VE" where I want to create Virtual environment. 1.我创建一个VSCode文件夹-“ D:\\ Code_Python_VE”,在其中创建虚拟环境。
2. Next I type the command - "pip3 install virtualenv". 2.接下来,我键入命令-“ pip3 install virtualenv”。 (D:\\Code_Python_VE> pip3 install virtualenv ) 3. D:\\Code_Python_VE> python3 -m venv project_env (D:\\ Code_Python_VE> pip3 install virtualenv)3. D:\\ Code_Python_VE> python3 -m venv project_env
4. D:\\Code_Python_VE>project_env\\Scripts\\activate.bat 4. D:\\ Code_Python_VE> project_env \\ Scripts \\ activate.bat
5. D:\\Code_Python_VE> ls - This will list a new directory "project_env". 5. D:\\ Code_Python_VE> ls-这将列出一个新目录“ project_env”。
6. D:\\Code_Python_VE> code . 6. D:\\ Code_Python_VE>代码。 This will start Visual Studio Code. 这将启动Visual Studio代码。 Make sure the command is (code .). 确保命令是(代码。)。
7. Create launch.jason with following content: 7.创建具有以下内容的launch.jason:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "python",
            "request": "launch",
            "name": "Python: Current File (Integrated Terminal 1)",
            "program": "${file}"
        },
        {
            "name": "Python: Current File (Integrated Terminal 2)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
    ]
}

(Please search how to go to Debug window and Add new Configuration in VS Code). (请搜索如何转到“调试”窗口并在VS Code中添加新配置)。

  1. Press F1 in Visual studio code and the command pallet will open - Select Python Interpreter and select the virtual environment project_env. 在Visual Studio代码中按F1键,命令托盘将打开-选择Python Interpreter,然后选择虚拟环境project_env。
  2. Add test.py file with one statement print("Hello World"). 使用一条语句print(“ Hello World”)添加test.py文件。
  3. Run this program. 运行该程序。
  4. In Visual studio Code terminal - 在Visual Studio Code终端中-
    (project_env) d:\\Code_Python_VE>python -m pip install --upgrade (project_env)d:\\ Code_Python_VE> python -m pip install --upgrade
    I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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