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
do not add directory of sys.argv[0] into sys.path #40210
Comments
Python adds magically directory of sys.argv[0] into >>> import sys
>>> sys.path
['', '/usr/lib/python23.zip', '/usr/share/python2.3',
'/usr/share/python2.3/plat-linux2',
'/usr/share/python2.3/lib-tk',
'/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages',
'/usr/lib/python2.3/site-packages/gtk-2.0',
'/usr/share/python2.3/site-packages'] where '' (or /usr/bin when executed script is in It is useful in many circumstances but fails when name The solutions:
The 1. is a tricky and not intuitive and quite funny: The 2. is a dirty hack: hey, we gonna import system The 3. is, IMHO, not acceptable because there is more The 4. is only acceptable, IMHO. It makes python more Maybe there is another solution, but... Patch attached. |
Logged In: YES Would not this cause serious backward compatibility problems?? |
Logged In: YES I've been bitten by this before. See e.g. the shtoom.py |
Logged In: YES This "problem" will be fixed in Python 2.4 with the http://www.python.org/peps/pep-0328.html As stated in the PEP, to use the obviously backwards from __future__ import absolute_import |
Logged In: YES wrobell, would you be willing to produce a version of the |
Logged In: YES i will not provide the patch for 328, so closing this issue |
Logged In: YES I am opening it again to discuss it a little more... How PEP-328 is going to solve problem I have described? If I name my script email.py, which will try to import email |
Logged In: YES If the entirety of PEP-328 made it into Python 2.4 (I don't Is this not clear by reading PEP-328? |
Logged In: YES But the problem is not with naming of my modules/packages For example consider script: #!/usr/bin/python
import email
print 1 And name the script email.py, then run it, please. Python |
Logged In: YES If you were to make your 'email.py' file contain the #!/user/bin/python
from __future__ import absolute_import
import email
print 1 It should import the email package. |
Logged In: YES That doesn't follow at all. The script email.py will _still_ be found first instead While a common newbie gotcha, I don't think it's worth the trouble to try to |
Logged In: YES Another point - given a program which comprises a couple of Fixing this would involve adding something like sys.path.insert(0,
os.path.dirname(os.path.abspath(sys.argv[0])) at the top of my main script. I'd hate to try to explain The use case here seems to be when a script itself has the |
Logged In: YES I do understand that the patch will not be accepted. :-) But, let's see what common script names are forbidden now And in the future there can be even more depending on the IMHO, it is a problem and should be fixed. The question is http://hkn.eecs.berkeley.edu/~dyoo/python/__std__/ I would set Resolution to "Remind" if you agree to discuss |
Logged In: YES I'm sorry, the bit I quoted shouldn't go into your email.py Standard library name masking is exactly what the absolute This /does/ in fact fix the problem mentioned, at the cost It also doesn't require any patches. |
Logged In: YES
Only in the context of submodule imports within packages. Which is _not_ at |
Logged In: YES Absolute imports will also fix that. A bare "from |
Logged In: YES
No it won't! The directory containing email.py is on sys.path, at the front.
Nothing at all is solved by a change that isn't in Python 2.4! I still think this bug should be closed won't fix. |
Logged In: YES A literal reading of "Guido's Decision" in PEP-328 says that I finally got around to installing 2.4, and (unfortunately) |
Logged In: YES
I don't think this __init__.py file had been mentioned before. However,
It's awaiting an implementation, AFAIK. |
Logged In: YES It's not hard to understand, but I could have sworn that in It seems as though I was mistaken as to the reasons behind |
Logged In: YES Seems like the main motivation for the original patch was to Would emitting a warning (with an easy way to explicitly I am not sure whether such a warning should be limited to |
Logged In: YES This is too problematic to change the behavior. Closing as |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: