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

Remove script dir from sys.path? #203

Closed
robnagler opened this issue Jun 17, 2022 · 2 comments
Closed

Remove script dir from sys.path? #203

robnagler opened this issue Jun 17, 2022 · 2 comments
Assignees

Comments

@robnagler
Copy link
Member

Try this from any directory (not in sys.path):

echo 'import csv;csv.reader' > csv.py
python csv.py

It will give this error: AttributeError: module 'csv' has no attribute 'reader'

@garsuga ran into this error, and I was confused by it. I now see that sys.path gets PWD inserted. This broke @garsuga's code that we had to stop using the program, because sirepo.numpy exists. (The program cd'd to the module being debugged, which is a separate issue.)

I think we should consider dropping the first element in sys.path in pykern.pkcli. (We should also add a fixup to projex that imports everything unqualified to reduce [but not eliminate] collisions.)

This has been known in Unix since 1984.

Go recently added something to prevent this. I think this is a breaking change.

Since all processes are started with pykern.pkcli.main (except sirepo_jupyterhub which could be changed), it could strip '.' from the first element of sys.path. I think this would avoid problems and be more secure.

@e-carlin
Copy link
Member

Sounds good. FWIW they've added a flag and env var in 3.11 python/cpython#31542

@robnagler robnagler changed the title Remove PWD from sys.path? Remove script dir from sys.path? Jun 22, 2022
@robnagler
Copy link
Member Author

Thanks for the link @e-carlin. It led me to _PyPathConfig_ComputeSysPath0, which computes syspath0. The function with documentation is 142 lines long. More importantly it uses readlink. Furthermore, py.test inserts pwd in sys.path. Kind of messy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants