Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conda activate from subprocess #23367

Closed
balazon opened this issue May 7, 2024 · 6 comments
Closed

conda activate from subprocess #23367

balazon opened this issue May 7, 2024 · 6 comments
Assignees
Labels
triage-needed Needs assignment to the proper sub-team

Comments

@balazon
Copy link

balazon commented May 7, 2024

Type: Bug

Behaviour

I'm trying to do a simple thing where I activate another environment and run some code in that environment, using subprocess.check_call.
I know I should be able to do that without conda activate myenv, and then running my command with conda run command.
But the problem is conda command is not found (not in PATH?) somehow. So I can't even do a version check, much less something like running a command in a different environment.
I get FileNotFoundError: [Errno 2] No such file or directory: 'conda'

Is there a workaround for this? Is this expected behavior? It's not my usual workflow I know. Usually I just run commands in a single environment, and that's it, but this just came up, so I'm wondering if it's possible.

Steps to reproduce:

  1. just call conda --version (or any conda command) from a subprocess:
import subprocess
if __name__ == "__main__":
    subprocess.check_call(["conda", "--version"])

I expect something like
conda 23.3.1
as my output which works in a separate terminal window, but in vscode terminal I get:

Traceback (most recent call last):
  File "/home/ad.adasworks.com/balint.varga/work/automat/lidar_autoannot/myscript.py", line 3, in <module>
    subprocess.check_call(["conda", "--version"])
  File "/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/lib/python3.10/subprocess.py", line 364, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/lib/python3.10/subprocess.py", line 345, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'conda'

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2024-05-07 16:59:29.205 [info] Experiment 'pythonRecommendTensorboardExt' is active
2024-05-07 16:59:29.205 [info] Experiment 'pythonREPLSmartSend' is active
2024-05-07 16:59:29.205 [info] Experiment 'pythonSurveyNotification' is active
2024-05-07 16:59:29.205 [info] Experiment 'pythonTerminalEnvVarActivation' is active
2024-05-07 16:59:29.205 [info] Experiment 'pythonTestAdapter' is active
2024-05-07 16:59:29.205 [info] Default formatter is set to null for workspace /home/ad.adasworks.com/balint.varga/work/automat/lidar_autoannot
2024-05-07 16:59:29.205 [info] Python interpreter path: ~/miniforge3/envs/py/bin/python
2024-05-07 16:59:29.365 [info] > pyenv which python
2024-05-07 16:59:29.365 [info] cwd: .
2024-05-07 16:59:29.714 [info] > conda info --json
2024-05-07 16:59:29.714 [info] shell: bash
2024-05-07 16:59:30.461 [info] Shell integration status changed, can confirm it's working.
2024-05-07 16:59:31.042 [info] > conda run -n py --no-capture-output python ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariables.py
2024-05-07 16:59:31.042 [info] shell: bash
2024-05-07 16:59:31.042 [info] > conda run -n py --no-capture-output python ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariables.py
2024-05-07 16:59:31.042 [info] shell: bash
2024-05-07 16:59:31.525 [info] Starting Pylance language server.
2024-05-07 16:59:32.320 [info] > /usr/bin/python3 ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariables.py
2024-05-07 16:59:32.320 [info] shell: bash
2024-05-07 16:59:32.364 [info] Setting environment variable _CE_M in collection to  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable CONDA_ROOT in collection to /home/ad.adasworks.com/balint.varga/miniforge3 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable CONDA_PREFIX in collection to /home/ad.adasworks.com/balint.varga/miniforge3/envs/py {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable CONDA_PROMPT_MODIFIER in collection to (py)  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable _CE_CONDA in collection to  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable CONDA_SHLVL in collection to 1 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.364 [info] Setting environment variable CONDA_DEFAULT_ENV in collection to py {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.365 [info] Prepending environment variable PATH in collection with /home/ad.adasworks.com/balint.varga/miniforge3/envs/py/bin: {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.365 [info] Prepending environment variable PS1 in collection with (py)  {"applyAtShellIntegration":true,"applyAtProcessCreation":false}
2024-05-07 16:59:32.367 [info] Send text to terminal: /usr/bin/python3 /home/ad.adasworks.com/balint.varga/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariablesToFile.py /home/ad.adasworks.com/balint.varga/.vscode/extensions/ms-python.python-2024.6.0/python_files/deactivate/bash/envVars.txt
2024-05-07 16:59:32.391 [info] Setting environment variable _CE_M in collection to  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable CONDA_ROOT in collection to /home/ad.adasworks.com/balint.varga/miniforge3 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable CONDA_PREFIX in collection to /home/ad.adasworks.com/balint.varga/miniforge3/envs/py {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable CONDA_PROMPT_MODIFIER in collection to (py)  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable _CE_CONDA in collection to  {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable CONDA_SHLVL in collection to 1 {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Setting environment variable CONDA_DEFAULT_ENV in collection to py {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Prepending environment variable PATH in collection with /home/ad.adasworks.com/balint.varga/miniforge3/envs/py/bin: {"applyAtShellIntegration":true,"applyAtProcessCreation":true}
2024-05-07 16:59:32.391 [info] Prepending environment variable PS1 in collection with (py)  {"applyAtShellIntegration":true,"applyAtProcessCreation":false}
2024-05-07 16:59:35.209 [info] > conda info --json

Extension version: 2024.6.0
VS Code version: Code 1.89.0 (b58957e67ee1e712cebf466b995adf4c5307b2bd, 2024-05-01T02:08:25.066Z)
OS version: Linux x64 6.2.0-26-generic
Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.13
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Conda
  • Value of the python.languageServer setting: Default
User Settings


languageServer: "Pylance"

Installed Extensions
Extension Name Extension Id Version
C/C++ ms-vscode.cpptools 1.19.9
Edit csv janisdd.vscode-edit-csv 0.9.1
Even Better TOML tamasfe.even-better-toml 0.19.2
JavaScript Debugger ms-vscode.js-debug 1.89.0
JavaScript Debugger Companion Extension ms-vscode.js-debug-companion 1.1.2
Pylance ms-python.vscode-pylance 2024.5.1
Python ms-python.python 2024.6.0
Python Debugger ms-python.debugpy 2024.6.0
Rainbow CSV mechatroner.rainbow-csv 3.11.0
Remote - SSH ms-vscode-remote.remote-ssh 0.110.1
Remote - SSH: Editing Configuration Files ms-vscode-remote.remote-ssh-edit 0.86.0
Remote Explorer ms-vscode.remote-explorer 0.4.3
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
System Info
Item Value
CPUs Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz (8 x 3399)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) 1, 1, 1
Memory (System) 31.29GB (25.27GB free)
Process Argv --crash-reporter-id ba19b503-1988-4abe-b16c-d9b84b97d151
Screen Reader no
VM 0%
DESKTOP_SESSION ubuntu
XDG_CURRENT_DESKTOP Unity
XDG_SESSION_DESKTOP ubuntu
XDG_SESSION_TYPE x11
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscoreces:30445986
vscod805cf:30301675
binariesv615:30325510
vsaa593cf:30376535
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
2i9eh265:30646982
962ge761:30959799
pythongtdpath:30769146
welcomedialog:30910333
pythonidxpt:30866567
pythonnoceb:30805159
asynctok:30898717
pythontestfixt:30902429
pythonregdiag2:30936856
pyreplss1:30897532
pythonmypyd1:30879173
pythoncet0:30885854
h48ei257:31000450
pythontbext0:30879054
accentitlementsc:30995553
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
cppperfnew:31000557
dsvsc020:30976470
pythonait:31006305
chatpanelc:31018788
dsvsc021:30996838
da93g388:31013173
a69g1124:31038041
pythonprc:31039816
dwnewjupytercf:31039676

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label May 7, 2024
@karthiknadig
Copy link
Member

@balazon Can you share what the PATH environment variable looks like between external terminal and VS Code terminal?

@karthiknadig karthiknadig self-assigned this May 7, 2024
@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 7, 2024
@balazon
Copy link
Author

balazon commented May 7, 2024

external terminal PATH:
/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/bin:/home/ad.adasworks.com/balint.varga/miniforge3/condabin:/home/ad.adasworks.com/balint.varga/cuda/cuda-11.8/bin:/home/ad.adasworks.com/balint.varga/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

vscode terminal PATH:
/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/bin:/home/ad.adasworks.com/balint.varga/cuda/cuda-11.8/bin:/home/ad.adasworks.com/balint.varga/miniforge3/envs/py/bin:/home/ad.adasworks.com/balint.varga/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

Interesting is that conda is here:
/home/ad.adasworks.com/balint.varga/miniforge3/condabin/conda
Which is found in the external terminal PATH, but nowhere in the vscode terminal PATH I think. Still the conda command totally works in the vscode terminal, unless I try to use it from inside my python script using subprocess.
Vscode terminal which conda output is totally empty. I can't find it in aliases either. Vscode has some magic that still allows me to use it

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label May 7, 2024
@karthiknadig
Copy link
Member

@balazon It looks like the conda path is probably getting added via .bashrc or equivalent for your terminals. But VS Code itself is launched in a way where that is not available to it. Try this from your external terminal:

> cd <your project dir>
> code .

That should use the same PATH variable as your external terminal, and then the behavior should match.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 9, 2024
@balazon
Copy link
Author

balazon commented May 9, 2024

This works yes thank you. Just curious though: how does vscode still manage to find conda after not having it anywhere in PATH, when I launch it from applications, and not from terminal?

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label May 9, 2024
@karthiknadig
Copy link
Member

It looks for PATH first, then has an internal known paths list that it goes through for each platform. If it can't find there, then it reverse engineers the path to conda by looking at environment.txt in your HOME (~/.conda/environment.txt).

@balazon
Copy link
Author

balazon commented May 10, 2024

Thank you for clearing that up, I suppose this issue can be closed

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

3 participants