-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
PR: Append paths that come from Spyder's Python path manager to the end of sys.path
#378
Conversation
3f24bdf
to
bd1b5bd
Compare
aee83d7
to
351bae6
Compare
…vior. Note that <env>/bin path is not included here. Update the kernel's os.environ['PYTHONPATH'] to match new paths.
351bae6
to
f374a49
Compare
The image you show illustrates the current behavior. If you deactivate the path and reactivate it, you'll find that the position of the path moves to just under python37.zip. With this PR, the initial position should be at the top of |
Why? I think that's not what we want because then the paths that come from our Python path manager would shadow standard library modules, which is really bad. Instead, they should be at the bottom by default, i.e. below all important directories. However, if the user enables an option on the Spyder interface to add them at the top, then we should do that. |
Because that is the standard Python/IPython behavior. See https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH.
I think this is a risk that all Python users face and is not unique to Spyder or Spyder users. I think it is within the scope of Spyder to protect itself from users shadowing standard libraries (e.g.
I did not intend to add a new feature with this PR, only to enforce consistent |
@CAM-Gerlach, you have a lot of experience with Python standards; do you have any opinion on this topic? Perhaps there is something that I'm missing. |
Even if this is the standard behavior, I think it's a bad practice that can catch a lot of newbies off guard and without a simple way to understand how to solve problems generated by this. Using Scientific Python is hard enough as it is to add more hurdles for users.
Actually, we do a lot of things to protect them. For instance, we already prevent user modules to shadow standard library ones. Since that's our current practice, I don't agree with changing it. Another argument against this change is that many, many users copy/paste random instructions they find on the web to solve installation issues without understanding how Python or
As I said, we can add an option to Spyder so that power users who want to enforce the standard behavior can do it. But I'm strongly -1 on this being the default. |
Additionally, I think this PR doesn't work as intended given the screenshot I posted above. Instead, this should be moved to Spyder and set the |
I think I may have incorrectly cloned the |
I added the modal window as suggested on the latest push as well. |
Where do we already prevent user modules from shadowing standard library ones? I'm aware that we do that to shield IPython Console (SPY_PYTHONPATH), and we take steps to protect Jedi and the PyLS, but where do we do it to protect the users own code execution?
While I respectfully disagree that we need to prevent them from shooting themselves in the foot, I will be happy to add this feature to this PR with the default set to append |
Unless it would be easier to do this in a followup PR since that will require adding a configuration setting. Let me know which you prefer. |
Ryan: "I would like to propose—" The ideal solution is a checkbox in the Python path manager that controls whether the paths are inserted or extended. But if that's not immediately practical, I would recommend sticking with the standard Python behavior in this particular instance, because:
I certainly run into my fair share of newbies shadowing stdlib modules, in fact I just helped one a couple days ago. However, this that this is an opt-in advanced feature that's very explicitly stated to be only for power users who are comfortable with how the Python search path worked to begin (and we can add even more blaring warnings if needed), who would immediately recognize what had happened if they shadowed a stdlib module and did not intend it if anything because it is a well known beginner mistake (while one rarely if ever sees non-beginners confused by it). If users still ignore all the warnings and use this fairly difficult to accidentally discover advanced feature anyway and ignore all the warnings, then they can still cause other trouble with it, and if we do add a checkbox, there's nothing technically stopping them from clicking it either if they've made it that far. And at least if things do still break in spite of all of this, it will be in a way consistent with the other 98% of the Python ecosystem so that any help they find will be more useful and relevant.
Thanks to @mrclary 's spyder-ide/spyder#17512 , we already do that by not using the
The current directory/project path is appended to the path, not injected at |
@mrclary, the problem is that there are two different behaviors now (I checked again with your latest changes):
This is an UX inconsistency that we need to solve so that things are uniform (i.e. paths are always added to the top or bottom, regardless of the situation). That's why I suggested to use a kernel handler (now I see we have one, called By the way, that would make useless the code you changed here in
I was referring to that, sorry for the misunderstanding.
Ok, thanks for understanding. Then, please work on your Spyder PR to add what @CAM-Gerlach said:
with that checkbox being disabled by default.
The problem is that Spyder makes way too easy to alter That's why I think we need to be really conservative about this change. |
@ccordoba12 I am unable to reproduce this behavior. For me, new IPython Consoles at Spyder launch as well as new Console tabs place the PPM paths at the top of |
Let me know if you want/need me to test something on Windows...
Just to be clear, did you open a new console, then go into the PYTHONPATH manager and add a new path, then click OK, then check the console again? Or only test with the existing PYTHONPATH manager paths without changing them after the console was started (or are they only intended to take effect on console start)?
I mean, what said above that I was responding to was that it is way too easy for newbie programmers to copy code they found online to set env variables, modify PYTHONPATH and shadow things, which @mrclary 's changes prevents. Now you're saying the Spyder makes it too easy to set the PYTHONPATH internally, but like I said, if that's a problem we can add a red-letter warning in the dialog text, and/or a modal warning dialog they have to click through when they click the "insert at the front of path" checkbox.
Yes, but this only affect running Python outside of Spyder, where it makes no difference where Spyder inserts the path internally, and furthermore it means that things would suddenly break once they try to run their code outside of Spyder as opposed to immediately after making the change and consistently between Spyder and elsewhere, which is potentially much more difficult for users to figure out and debug. |
Yes. See below screen capture. The initial startup console behaves correctly as well as new consoles, in multiple environments, and after updating in PPM. So I'm still not sure what circumstances produced @ccordoba12's screenshot.
I'm not sure, exactly, what my changes prevent that you're referring to here. I don't think it is a problem to make it "too easy" to change PYTHONPATH (PPM). These are just extra search paths; even newbies may need to add a local directory to their Python search path. Regarding "red-letter warnings", while it is useful to be informative to users, I think this would inflate the threat.
Yes, I think this is just the risk of treating extra search paths in Spyder differently than the standard Python/IPython protocol. With the added feature of #18308, we should have clear messaging about the affects of prepending/appending PYTHONPATH to sys.path. |
OS-specific differences? Did you try on Binder?
Well, they shouldn't, at least if they're new; Spyder automatically adds the current working directory (project dir, etc) so if they are working directly in that, their own packages/modules will work fine. If they have other Python library code they need to access, they should just install them properly rather than doing manual path hacking, unless they are an experienced developer (like you) who fully understand what they are doing and have a specific need to do it.
Yes, indeed, at least to the extent we're exposing the equivalent of standard Python features and calling them such (this doesn't necessarily extend as far as replicating Python's discouraged |
My company employs 50+ physicists, many of which use Spyder, and many of which I consider "newbies". We have a set of proprietary Python tools that we distribute via git. So yes, "newbies" should have extra search paths. For those that don't use Spyder, we have to advise them to modify their system PYTHONPATH. For those that do use Spyder, the PPM is thankfully much easier. Perhaps someday I'll create whls for these Python tools and they can just install them and not worry about PYTHONPATH, but that is far down the priority list. |
How do I do that for a specific pull request? |
/show binder |
I don't know what that means... |
That was supposed to trigger our GHA bot to post a comment with the generated Binder link to launch it on this PR, but it seems it didn't work. @ccordoba12 do you know what's going on? It seems the binder chat workflow never triggered even though it looks right in the workflow YML. |
Maybe you meant to do that in the spyder repo instead of here in the spyder-kernels repo? |
🤦 |
I still cannot reproduce @ccordoba12's screen shot. I've tried on my Ubuntu VM and still get the expected behavior with this PR (in conjunction with #17512). |
sys.path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this locally and it's working as expected. Thanks @mrclary!
Update
os.environ['PYTHONPATH']
along withsys.path
.PYTHONPATH
paths are inserted at position 0 instead of position 1 onsys.path
update because the<env>/bin
path is not included (why?), consistent with expected IPython behavior.See spyder-ide/spyder#17512