Skip to content

Auto-select python from the activated environment #14918

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

Closed
1MLightyears opened this issue Dec 9, 2020 · 12 comments
Closed

Auto-select python from the activated environment #14918

1MLightyears opened this issue Dec 9, 2020 · 12 comments
Assignees
Labels
area-environments Features relating to handling interpreter environments feature-request Request for new features or functionality needs PR Ready to be worked on

Comments

@1MLightyears
Copy link

Environment data

  • VS Code version: 1.51.1
  • Extension version (available under the Extensions sidebar): (what is that? not finding)
  • OS and version: Windows 10 1909 (18363.1198)
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.8 (Anaconda)
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Relevant/affected Python packages and their versions: PySide2==5.15.2, sip==5.5.0
  • Relevant/affected Python-related VS Code extensions and their versions: Anaconda Extension Pack==v1.0.1, Python==v2020.11.371526539, Qt for Python==v0.5.3
  • Value of the python.languageServer setting: Jedi

[NOTE: If you suspect that your issue is related to the Microsoft Python Language Server (python.languageServer: 'Microsoft'), please download our new language server Pylance from the VS Code marketplace to see if that fixes your issue]

Expected behaviour

well i expected it to import PySide2, but it failed.

Actual behaviour

When running the python script with the automatically generated launch.json

Traceback (most recent call last):
  File "e:\Python\myenv\src\main.py", line 16, in <module>
    from draw import draw_window
  File "e:\Python\myenv\src\draw.py", line 13, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named 'PySide2'

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. Create a file (say, main.py)

  2. Input only one single line: import PySide2

  3. Save and change to the virtual env, run with the debugger provided by Python extension

  4. You'll see the ModuleNotFoundError: No module named 'PySide2'

  5. Running python main.py doesn't raise this error

  6. Running import PySide2 in python interactivate mode doesn't raise this error

  7. I'm pretty sure that I DID pip install PySide2 in my virtual env, and pip show PySide2 DOESN'T raise WARNING: Package(s) not found, so i suspect it a problem has something to do with the extension.

Logs

I post the output of pip freeze of my env here:

certifi==2020.12.5
packaging==20.7
pyparsing==2.4.7
PyQt5==5.15.2
PyQt5-sip==12.8.1
PySide2==5.15.2
pywin32==225
shiboken2==5.15.2
sip==5.5.0
toml==0.10.2
wincertstore==0.2

@1MLightyears 1MLightyears added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Dec 9, 2020
@karthiknadig
Copy link
Member

This is a known issue with activating conda environments (#5559). This occurs due to some limitation we have with the VS Code terminals and conda. Here is a workaround for this:

  1. Turn off auto activation. To do this, disable these two settings, ideally only for the project you are working on:
    image
  2. Exit VS Code.
  3. Open Anaconda Prompt, navigate to your project.
  4. Activate the environment that you want.
  5. Run code . or code project.code-workspace to launch VS Code.

This will give you an activated environment, and the extension will not have to try and activate every time a terminal is created.

@karthiknadig karthiknadig self-assigned this Dec 9, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Dec 9, 2020
@1MLightyears
Copy link
Author

hi @karthiknadig
thank you for your reply, but I'm pretty sure that my case is different from #5559.
It seems that #5559 happens because terminal starts much faster before python extension could load itself.
But I found this like, the 1st or 2nd time using Python extension, so I always load the extension manually.
The result is that I DO have the anaconda env prompt (like (base)e:\ stuff) in my vscode terminal, and it still failed to import PySide2.
And the solution you provided failed either - I cancel the ticks, opened anaconda prompt, conda activate-ed my env, cd to my workspace and code ., debugging...

ModuleNotFoundError: No module named 'PySide2'

urgh......

@karthiknadig
Copy link
Member

@1MLightyears Can you confirm that the selected python is same as the one where you have PySide2 installed?

@1MLightyears
Copy link
Author

@karthiknadig Yes I'm pretty sure.
my prompt is like

(clarisse) E:\Python\Clarisse\src>

run pip list, it shows

(clarisse) E:\Python\Clarisse\src>pip list
Package      Version
------------ -------------------
certifi      2020.12.5
packaging    20.7
pip          20.3.1
pyparsing    2.4.7
PyQt5        5.15.2
PyQt5-sip    12.8.1
PySide2      5.15.2
pywin32      225
setuptools   49.6.0.post20201009
shiboken2    5.15.2
sip          5.5.0
toml         0.10.2
wheel        0.36.1
wincertstore 0.2

run without debugging it shows

(clarisse) E:\Python\Clarisse\src> cd e:\Python\Clarisse\src && cmd /C "D:\Anaconda3\python.exe c:\Users\{my name}\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher 53386 -- e:\Python\Clarisse\src\main.py "
Traceback (most recent call last):
  File "e:\Python\Clarisse\src\main.py", line 16, in <module>
    from draw import draw_window
  File "e:\Python\Clarisse\src\draw.py", line 13, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named 'PySide2'

but if i use

(clarisse) E:\Python\Clarisse\src>python main.py

(clarisse) E:\Python\Clarisse\src>

it ran without raising error!

@karthiknadig
Copy link
Member

@1MLightyears Can you see that debugger is running D:\Anaconda3\python.exe? can you run this command from your activated environment:

(clarisse) E:\Python\Clarisse\src>python -c "import sys;print(sys.executable)"

see if they match.

I would expect the path to be something like "D:\Anaconda3\envs\clarisse\python.exe" since conda would be using the python.exe from the environment folder, not the root or base conda.

@1MLightyears
Copy link
Author

1MLightyears commented Dec 10, 2020

@karthiknadig ok I know that the problem occurs in the command line, something is wrong when calling debugpy.
I'm thinking of changing this issue into a feature request, since I think it's better to generate the command line based on the current env (someone like me would usually change them when switching from different works).

@karthiknadig
Copy link
Member

@1MLightyears Did the command I shared give a different path? Can you share your launch configuration for the debugger? Does the debugger config by chance set the pythonPath itself? I think the problem here is that the extension is not using the python.exe from activated environment (this might be a known issue with activated conda envs), but I don't know about this enough to confirm that.

@1MLightyears
Copy link
Author

1MLightyears commented Dec 11, 2020

@karthiknadig yes you're right the extension isn't using the python.exe from the activated env, but the python.exe of the base env, and looks like it keeps using the python.exe from base env despite my switching to another env.

(clarisse) E:\Python\Clarisse\src>python -c "import sys;print(sys.executable)"

does give D:\Anaconda3\envs\clarisse\python.exe.
I'm wondering if this issue is about to be fixed or kept.

@karthiknadig
Copy link
Member

@1MLightyears In your settings do you have python.pythonPath set? check both the user and folder settings. If so try removing the entry, reload VS Code, then use the Python: Select Interpreter command to select the desired python which is D:\Anaconda3\envs\clarisse\python.exe.

If this does not work. Then please share the python extensions logs from Output > Python tab:
image

@1MLightyears
Copy link
Author

@karthiknadig thanks, I cleaned python.pythonPath and selected the interpreter of my env in Python: Select Interpreter, now the extension is running in the env interpreter and conda activate is activating my env after complete loading itself.
My question is, will it be supported that the interpreter could follow conda to change the interpreter automatically by monitoring some environment variables like CONDA_PREFIX or some other ways.

@karthiknadig
Copy link
Member

karthiknadig commented Dec 11, 2020

Yes. The extension should be able to select the python based on activated environment or CONDA_PREFIX.

We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision.

@karthiknadig karthiknadig added area-environments Features relating to handling interpreter environments needs decision feature-request Request for new features or functionality and removed triage bug Issue identified by VS Code Team member as probable bug labels Dec 11, 2020
@karthiknadig karthiknadig changed the title Fail to import PySide2 when using Python extension debugging Auto-select python from the activated environment Dec 11, 2020
@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@karrtikr
Copy link

Closing in favor of #18178

@karrtikr karrtikr closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments feature-request Request for new features or functionality needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

4 participants