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

Terminal doesn't activate selected conda environment #22068

Closed
suipz opened this issue Sep 23, 2023 · 22 comments · Fixed by #22082
Closed

Terminal doesn't activate selected conda environment #22068

suipz opened this issue Sep 23, 2023 · 22 comments · Fixed by #22082
Assignees
Labels
area-terminal author-verification-requested Issues potentially verifiable by issue author bug Issue identified by VS Code Team member as probable bug experimenting Feature is part of an experiment verified Verification succeeded

Comments

@suipz
Copy link

suipz commented Sep 23, 2023

Type: Bug

Behaviour

Expected vs. Actual

I am using Windows 10 and Anaconda for virtual environments.
recently, I change from spyder to vs code. it works fine.
But yesterday, when I started a new terminal or run a python file, new terminal did not activate the selected virtual env (Conda).
It DO work before. But didn't after I installed the follows on Friday.

  1. New Python (3.9.9) outside of Anaconda to test a simple web server.
  2. WSL 2 and Docker for RATH (https://rath.kanaries.net/).

currently, I tried to uninstall New Python (3.9.9) and reinstall vs code (after delete everything, it's a clean new install), but still doesn't work.
I know this is the same issue as #20885, but I tried all solutions mentioned in # 20885 and failed.

XXX

Steps to reproduce:

  1. Install new Python (3.9.9) outside of Anaconda to test a simple web server.
  2. WSL 2 and Docker for RATH (https://rath.kanaries.net/).

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.10
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Conda
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

XXX

User Settings


condaPath: "<placeholder>"

languageServer: "Pylance"

terminal
• executeInFileDir: "<placeholder>"

Extension version: 2023.16.0
VS Code version: Code 1.82.2 (abd2f3db4bdb28f9e95536dfa84d8479f1eb312d, 2023-09-14T05:55:25.390Z)
OS version: Windows_NT x64 10.0.19045
Modes:

System Info
Item Value
CPUs Intel(R) Xeon(R) Gold 5122 CPU @ 3.60GHz (8 x 3592)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
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
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 62.66GB (54.99GB free)
Process Argv --crash-reporter-id 72a488d0-fc85-4685-952b-366d42b0aba1
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscod805cf:30301675
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
pythonvs932:30410667
py29gd2263cf:30792227
vsclangdc:30486549
c4g48928:30535728
dsvsc012cf:30540253
pynewext54:30695312
azure-dev_surveyone:30548225
3biah626:30602489
89544117:30613380
showlangstatbar:30737416
962ge761:30841074
03d35959:30757346
pythonfmttext:30731395
fixshowwlkth:30771522
showindicator:30805244
pythongtdpath:30769146
i26e3531:30792625
pythonnosmt12:30797651
pythonidxptcf:30805731
pythonnoceb:30805159
copilotsettingt:30839829
synctok:30821570
dsvsc013:30795093
dsvsc014:30804076
diffeditorv2:30821572
pythonmpsinfo:30842941
dsvsc015:30829745

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Sep 23, 2023
@suipz
Copy link
Author

suipz commented Sep 23, 2023

I found a solution, although I don't know what's the reason for this issue.

I added "conda activate my-env-name" as
"${env:HOMEDRIVE}${env:HOMEPATH}\anaconda3 & conda activate my-env-name"
Before this revision, it's just
"${env:HOMEDRIVE}${env:HOMEPATH}\anaconda3"

The full related setting:
{
"terminal.integrated.defaultProfile.windows": "Anaconda Prompt",
"terminal.integrated.profiles.windows": {

    "Anaconda Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [
            "/k",
            "${env:HOMEDRIVE}${env:HOMEPATH}\\anaconda3\\Scripts\\activate.bat",
            "${env:HOMEDRIVE}${env:HOMEPATH}\\anaconda3 & conda activate my-env-name"                
        ],
        "icon": "terminal"
    }
}

}

@karrtikr
Copy link

karrtikr commented Sep 24, 2023

Please provide the debug logs after you start seeing the issue:

  • Run Developer: Set log level command
  • Select Python -> Select Trace
  • Run Developer: Reload Window command
  • Provide the output:
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

XXX

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 24, 2023
@suipz
Copy link
Author

suipz commented Sep 24, 2023

@karrtikr

Thanks a lot for the reply.
Please refer to attached PDF for the output.
As mentioned above, after added "conda activate my-env", it works fine.
It's output in this case in Pages 1-43,
Output.pdf

I deleted "conda activate my-env" and again terminal did not activate selected conda environment.
please find output in this case in Page 44-87.

Sorry for the long log.
Hope this could be useful to locate the issue.
Thanks again.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 24, 2023
@karrtikr karrtikr added the experimenting Feature is part of an experiment label Sep 24, 2023
@karrtikr
Copy link

Thanks. Couple things:

  • Try installing pre-release version of the extension and reload window:
    image
  • If that doesn't help, can you send your User settings? I'm wondering if you have shell integration turned off.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 24, 2023
@suipz
Copy link
Author

suipz commented Sep 25, 2023

@karrtikr

I switch to pre-release version and reload window, but it doesn't work.
It's still base env, but not selected virtual env.

image

I am not quite sure what kind of User setting you need.
Anyway, just send settings.json firstly.
If you need other info, please let me know.
Thanks.

image

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 25, 2023
@the-psychedelic
Copy link

the-psychedelic commented Sep 25, 2023

Hi @suipz

This is a very old problem that I have been facing at least on macOS since I started using virtual environments.

One working fix to this problem that I found was to change the "code-runner.executerMap" in settings.json under the key "python" to "$pythonPath $fullFileName".

I found setting "python.terminal.activateEnvironment": false; to be efficient in programs that take user input. Because it keeps on running till the environment gets activated (program takes this as the user input, thus causing a bug).

Although it does not solve the problem but all the code will run with the environment as selected in the interpreter path.
I understand it is a macOS issue, but if you use code-runner, this should solve the problem nonetheless.

Screenshot 2023-09-25 at 7 49 13 PM

The reason for this was, terminal would activate my environment after a delay, due to which the program runs without the environment being activated (it gets activated after that).

@karrtikr
Copy link

@suipz Can you also remove the "args" parameter activating conda base env from User settings? Alternatively please switch to default powershell profile using Select default profile command and create a new terminal.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 25, 2023
@suipz
Copy link
Author

suipz commented Sep 26, 2023

@the-psychedelic

Thanks for your reply.
I can solve the issue by user settings as mentioned above.
I just wonder why.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 26, 2023
@suipz
Copy link
Author

suipz commented Sep 26, 2023

@karrtikr

I tried again.

  1. Use "terminal.integrated.defaultProfile.windows": "Anaconda Prompt", but remove 'args'.
    It looks fine if I start a new terminal, selected virtual env activated. If I [Run Python File] from the button at right-upper editor after started a new terminal, It's also no problem, although the python file was run in a different terminal.
    however, if I [Run Python File] directly from the button at right-upper editor without manually started new terminal, it did not work since no selected virtual env activated. I mean, after I started vscode, I need to start new terminal once manually to activate virtual env. This cannot be done automatically when [Run Python File].

  2. Use "terminal.integrated.defaultProfile.windows": "PowerShell", it's same situation as No.1 mentioned above.

@karrtikr
Copy link

karrtikr commented Sep 26, 2023

Appreciate you giving it a try. If I understand correctly, once args is removed:

  • The issue is fixed for any new terminals created
  • The first terminal or existing terminal which shows up does not have the environment activated

if I [Run Python File] directly from the button at right-upper editor without manually started new terminal, it did not work since no selected virtual env activated.

I'm not very clear on this bit. I would ideally expect all terminals to behave the same once they're relaunched.

Can you dispose all terminals by clicking the 🗑️ icon and try it? Would really help if I could get some screenshots on how you're inferring whether the correct env was activated.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 26, 2023
@karrtikr
Copy link

Btw note, if you hover over terminal's name, it should also tell you which environment is currently activated:
image

@suipz
Copy link
Author

suipz commented Sep 26, 2023

@karrtikr

If I start a terminal by click Terminal menu/New terminal. It's OK and selected virtual env (py37) activated.
image

But if I don't do it, alternatively run python file directly after started vs code, library cannot be loaded since it's not installed properly under base env.
(if virtual env activated, it's no problem to load library.)

image

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 26, 2023
@suipz
Copy link
Author

suipz commented Sep 26, 2023

BTW, it's showed that py37 activated in the 2nd screenshot, but it's not in fact.

@suipz
Copy link
Author

suipz commented Sep 26, 2023

This is the screenshot to run the python file successfully if I started terminal in advance.
image

@karrtikr
Copy link

Thanks for the deets, we're very close. I'm able to reproduce the issue but under very specific conditions.

alternatively run python file directly after started vs code

Note environments take some time to activate (we don't a good job surfacing this, something we can improve), I think if you wait long enough before running the Python file (or opening a new terminal), you should not see the ⚠️ icon or this issue.

Also, this should only happen the first time an env is selected (and reasonable time is given for it to activate). Once you dispose all terminals with ⚠️ icon and reload window (Reload window command), the issue should not appear as long as py37 is selected.

@karrtikr
Copy link

If that still does not work for you, please provide the logs again #22068 (comment) after doing the steps needed to reproduce the issue.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 26, 2023
@suipz
Copy link
Author

suipz commented Sep 26, 2023

@karrtikr

I tested again. Yes, just as you said, it's no problem after waited for 15min.
maybe no need to wait 15min. I just did something else.
Anyway, thanks a lot for your confirmation.
This issue could be closed now, I think.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 26, 2023
@karrtikr
Copy link

No worries! I'll just make the quick fix to show a progress indicator to help you with how long it takes to re-activate the environment, after which we can close this. Thanks for the help.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 26, 2023
@suipz
Copy link
Author

suipz commented Sep 26, 2023

@karrtikr
Sorry, I am new to github.
Do you mean I could do something from #22082 to know the time for the activation of environment?
I don't know how to do. Please give me some guidance. Thanks.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Sep 26, 2023
@karrtikr
Copy link

Yep, you'll see an indicator on the bottom left which indicates how long it takes for activation an environment. Fix should be out in the pre-release version of the extension in a hour, use the following to try it out:

image

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Sep 26, 2023
@karrtikr karrtikr added bug Issue identified by VS Code Team member as probable bug area-terminal author-verification-requested Issues potentially verifiable by issue author and removed info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team labels Sep 26, 2023
@karrtikr
Copy link

Once you reload the window after installing it, you should be able to see the indicator.

@suipz
Copy link
Author

suipz commented Sep 26, 2023

@karrtikr

Thanks a lot for your help.
I switched to pre-release version and found it's very fast to re-load window. Maybe only 1 or 2 sec.
I even cannot see the indicator.
Also, I close the vs code and start again to try to reproduce the issue.
But I failed to reproduce it with pre-release version.

And then I switched back to release version and tried again.
I can reproduce the issue, and re-activation of environment need about 5-10 sec.
I think it's not a long time.

That's all. Thanks again.

@karrtikr karrtikr added the verified Verification succeeded label Sep 26, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-terminal author-verification-requested Issues potentially verifiable by issue author bug Issue identified by VS Code Team member as probable bug experimenting Feature is part of an experiment verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants