Syntax error when importing a file which starts with a number

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Mar 23 14:16:34 EDT 2009


En Mon, 23 Mar 2009 14:56:21 -0300, <simon.woolf at gmail.com> escribió:

> I don't suppose anyone has any idea why it seems to be impossible to
> import any file which starts with a number?  You get a syntax error,
> whether the file exists or not.

You don't import a file, you import a module. And a module name is an  
identifier:
http://docs.python.org/reference/simple_stmts.html#the-import-statement
Identifiers must begin with a letter or underscore.
(Standalone scripts that aren't supposed to be imported don't have to obey  
this rule, like 2to3.py)

> Is this just me, or has anyone else run into it?  Is it a known bug?
> (If so, I can't find it on a bug tracker or in any Google searches).

It's not a bug. Suppose you have a file '1.py' and you could write:

import 1
x = 1

Does x refer to the integer 1 or the module 1?

> It's a bit annoying, as I have an enforced naming scheme.  Any way
> round it?

You might use a prefix in all these modules. Or just an _ in front of the  
ones that start with a number. But remember that a module name must obey  
both your filesystem rules for file names and Python rules for  
identifiers: letters A-Z (unless you're using Python 3), digits, _.

-- 
Gabriel Genellina




More information about the Python-list mailing list