How can I launch an exe within a conda env

2020-07-12 03:17发布

I'm using vscode in Windows 10 as my code editor, and want to make an easy way to launch it with the correct conda env to allow debugging.

Currently I am having to open a command prompt, then activate the conda env, then paste the shortcut to vscode into the prompt to execute. Like so:

cmd
activate env-name
"C:\Program Files (x86)\Microsoft VS Code\Code.exe"

I have tried creating a batch file to wrap these calls, but unfortunately once I call "source activate" to start the conda env, the batch commands after this are not executed as it is considered another instance.

Any tips? Other than writing a vscode extension to handle this (which I'm seriously tempted to do, but it's such a simple problem...)

3条回答
萌系小妹纸
2楼-- · 2020-07-12 03:51

The best option I found is to set the python.venvPath parameter in vscode settings to your anaconda envs folder.

"python.venvPath": "/Users/[...]/Anaconda3/envs"

Then if you bring up the command palette (ctl + shift + P on windows/linux, cmd + shift + P on mac) and type Python: Select Workspace Interpreter all your envs will show up and you can select which env to use.

The python extension will also need to be installed for the Select Workspace Interpreter option.

Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode. My answer was originally posted here.

查看更多
叛逆
3楼-- · 2020-07-12 03:51

Using Conda 4.7.5, I was able to change the Target under the VsCode taskbar shortcut Properties from:

"C:\Users\Paul.siersma\AppData\Local\Programs\Microsoft VS Code\Code.exe"

To:

C:\Users\Paul.siersma\Anaconda3\_conda.exe run -p C:\Users\Paul.siersma\Anaconda3 "C:\Users\Paul.siersma\AppData\Local\Programs\Microsoft VS Code\Code.exe"

This uses the run command (marked experimental) and starts VSCode using the base Conda environment. You could specify another environment by changing the -p flag to the environment location eg -p [..]\Anaconda3\envs\myenv

查看更多
再贱就再见
4楼-- · 2020-07-12 04:04

You might want to run source activate env-name as a task in visual studio. https://code.visualstudio.com/Docs/editor/tasks

tasks.json

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "suppressTaskName": true,
    "args": [],
    "tasks": [
        {
            "taskName": "development",
            "args": ["source", "activate", "env-name"]
        }
    ]
}
查看更多
登录 后发表回答