简体   繁体   English

Visual Code 如何自动激活虚拟环境?

[英]Visual Code how can I activate the virtual environment automatically?

Maybe a simple question, but I cannot figure it out.也许是一个简单的问题,但我无法弄清楚。 I work within Visual Studio Code on a MacBook.我在 MacBook 上的 Visual Studio Code 中工作。 Within a directory I create a virtual environment and have activated that with source .venv/bin/activate scripts and everything works fine.在一个目录中,我创建了一个虚拟环境并使用源.venv/bin/activate脚本激活了它,一切正常。 In the terminal I see (.venv) (base) gaston@MacBook-Pro WebScraping %在终端我看到(.venv) (base) gaston@MacBook-Pro WebScraping %

When I start visual code another time and open this same directory I do not see the (.venv) so I again give manually the command to activate.当我再次启动可视代码并打开同一个目录时,我没有看到(.venv),所以我再次手动给出激活命令。

My question is there a way to activate the virtual environment automatically when opening the directory?我的问题是有没有办法在打开目录时自动激活虚拟环境?

VS Code Default Python Interpreter Path VS Code 默认 Python 解释器路径

You can do this in a number of ways.您可以通过多种方式做到这一点。 If your goal is to have the virtual environment selected immediately at the launch of VS Code, you will have to target that virtual environment as the Python Interpreter in VSC.如果您的目标是在 VS Code 启动时立即选择虚拟环境,则您必须将该虚拟环境定位为 VSC 中的 Python 解释器。

https://code.visualstudio.com/docs/python/environments https://code.visualstudio.com/docs/python/environments

System Preferences->Settings->Python: Default Interpreter Path

You'll want to point it to your virtualenv Python installation, likely something like:您需要将其指向您的 virtualenv Python 安装,可能类似于:

envFolder/bin/python3

VS Code Launch Configuration Python Interpreter VS Code 启动配置 Python 解释器

You can also set a specific launch configuration so that VSCode will still use your system Python instance by default;您还可以设置特定的启动配置,以便 VSCode 默认仍使用您的系统 Python 实例; however, specific launch configurations will have the virtual environment specified.但是,特定的启动配置将指定虚拟环境。

https://code.visualstudio.com/docs/python/debugging https://code.visualstudio.com/docs/python/debugging

Thanks, I did something within settings and now it works.谢谢,我在设置中做了一些事情,现在它可以工作了。 When I open VSC now it opens the directory and the right environment, just the way I wanted it.当我现在打开 VSC 时,它会按照我想要的方式打开目录和正确的环境。

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

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