Syntax error when importing a file which starts with a number

Miles semanticist at gmail.com
Mon Mar 23 19:16:24 CET 2009


On Mon, Mar 23, 2009 at 1:56 PM,  <simon.woolf at gmail.com> wrote:
> Hello, all.
>
> 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.

Identifiers can't start with a number.

http://docs.python.org/reference/lexical_analysis.html#identifiers

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

You could import it like so:

some_valid_identifer = __import__('1foo', globals(), locals())

http://docs.python.org/library/functions.html#__import__

But a far better solution is to fix your naming scheme—it's completely
illogical to have a Python module naming scheme where the names aren't
valid identifiers.

-Miles



More information about the Python-list mailing list