From: Guenter M. <mi...@us...> - 2011-10-31 20:40:06
|
On 2011-10-29, Paul Tremblay wrote: > Using the name math for a directory, or for a module, can lead to > unexpected results, namely because Python itself has a module named math. > If I am in trunk/docutils/docutils (which has a directory called math), > and type:: > python3 # points to Python 3.2 > I get the following error > Fatal Python error: Py_Initialize: can't initialize sys standard streams > File "io.py", line 96 > return decoded.replace(u'\ufeff', u'') > ^ > SyntaxError: invalid syntax > Abort trap: 6 I cannot reproduce this:: ~/Code/Python/docutils-svn/docutils/docutils > python3 Python 3.2.1rc1 (default, May 18 2011, 11:01:17) [GCC 4.6.1 20110507 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> > However, even with pyton 2.7, I can create a seemingly bizarre problem: > python > >>> import tempfile > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", > line 34, in <module> > from random import Random as _Random > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", > line 45, in <module> > from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil > as _ceil > ImportError: cannot import name log > Python is looking for log in math, but sees the math directory first, > and cannot import it. > I suggest we rename the math directory. > Likewise, I think I should rename the module math.py > (trunk/docutils/docutils/transforms/math.py) to something different, > such as math_rst.py I am not sure about the need for this change. What is the general recomendation for module/package names in Python? I am not aware of a "do not use standard module names for your sub-packages/sub-modules" rule. On the contrary, I've seen a recommendation to use "generic" names inside packages. if you really need to start a Python executable from this directory, you might need to delete the pwd from the first position in sys.path (either just delete or append instead of prepend). The jury is still out at http://stackoverflow.com/questions/1959188/absolute-import-failing-in-subpackage-that-shadows-a-stdlib-package-name The problem is handles in http://www.python.org/dev/peps/pep-0328/ and the solution are "absolute-imports" (available since 2.5) However, it states: As Python's library expands, more and more existing package internal modules suddenly shadow standard library modules by accident. It's a particularly difficult problem inside packages because there's no way to specify which module is meant. This implies that it is not wise to use the name of a standard module/package inside a custom package and we might e.g. use "mathematics" instead. Günter |