-
Notifications
You must be signed in to change notification settings - Fork 299
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
Packages are not imported from the selected Python environment #8553
Comments
Root cause is this when starting the kernel:
It looks like it's half using the global jupyter (which is probably my global jupyter interpreter) and the jupyter installed in the conda environment. |
Works for me, on windows. |
Yes I can. Everything works fine if I start jupyter from that environment. But if I start jupyter in another environment, it doesn't. |
Figured out the problem. Sys path is wrong. When working it's this:
When not working, the global sys path is injected first:
|
I wonder if we can generate a PYTHONPATH that fixes it. |
Yep that fixes it. @DonJayamanne I fixed it by updating how we print out variables. I changed this: import os
import builtins
import json
builtins.print("e976ee50-99ed-4aba-9b6b-9dcd5634d07d")
builtins.print(json.dumps(dict(os.environ))) to this import os
import builtins
import json
import sys
builtins.print("e976ee50-99ed-4aba-9b6b-9dcd5634d07d")
# Special case PYTHONPATH to include sys.path
formatted_sys_path = os.pathsep.join(sys.path)
os.environ["PYTHONPATH"] = f"{os.getenv('PYTHONPATH')};{formatted_sys_path}"
# Dump results
builtins.print(json.dumps(dict(os.environ))) Thoughts? I think this should be okay. We're not removing anything from the PYTHONPATH, and we're ensuring it includes the original sys path for the interpreter. |
Ideally this shouldn't be necessary, also most of the time PYTHONPATH is empty. Please can you check sys path for other python envs (virtual env) and other conda environments. |
I believe this should be harmless. PYTHONPATH would be redundant in most cases with what it figures out is sys.path. Although if we got it wrong it would certainly mess up the sys.path. |
Not sure what this means? You mean when printing? Only do this if you're on a virtual environment? |
|
I believe the path variables are wrong because jupyter is injecting its sys.path into the kernel on startup. I got those sys.path variables by changing the kernel launch command. I switched it from this:
to this
It would still crash, but then I could see the sys path. |
Not sure what you mean by this? What are they on my machine? Sys.path is what you expect. It includes the site-packages for the venv (or conda env). Conda envs seem to put their own paths first. |
I'm running into similar issues on Windows, this isn't specific to Mac M1 nor is it specific to Jupyter. This is what I'm getting
Basically the problem is the fact that The reason for other site_packages being searched (listed in sys.path) is outlined here https://docs.python.org/3/library/site.html#site.ENABLE_USER_SITE Note:
Users could be running into quite a lot of these as well. Problems
Solution
This is what we'd want to resolve 1, 2, & 3
|
I've submitted the PR and added tests for this, should be fine now. |
Notes
|
I verified Don's latest change. It works for me too. |
@DonJayamanne I saw that #8576 as merged and closed, but I'm not sure if it's still working or if something else needs to be changed on my end before the solution works. I had an issue where I updated plotly in Conda, but it's still showing an old version because it's using the global site packages. Screenshotted below is the output from sys.path and as you can see the conda site packages are prioritized below the global site packages. Is there something else I need to do? Currently running Python 3.9.12 and VS Code version 1.72.2. |
"jupyter.disableZMQSupport": "true"
Kernel never starts.
The text was updated successfully, but these errors were encountered: