How to import module whose filename starts number

Peter Otten __peter__ at
Wed Dec 12 18:57:29 CET 2012

Yong Hu wrote:

> I have a few scripts whose file names start with numbers. For example,
> I tried to import them in another script by "import 01_step1" or "from
> 01_step1 import *". Both failed, saying "SyntaxError: invalid syntax"
> Is there anyway to import those files? The file name must start with
> characters?

Or an underscore. The module name must be a valid identifier. In CPython you 
can hack around that restriction with 

step01 = __import__("01_step1")

but this "solution" is not portable and I recommend that you rename your 
scripts instead.

More information about the Python-list mailing list