How to import module whose filename starts number

Dave Angel d at davea.name
Wed Dec 12 12:53:27 EST 2012


On 12/12/2012 12:42 PM, Yong Hu wrote:
> I have a few scripts whose file names start with numbers. For example, 01_step1.py, 02_step2.py
>
> 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?

A module name is like any other symbol in Python.  It must start with a
letter (one of a hundred thousand or so), and have only letters or
digits within it.  Plus underscore, and maybe a couple more special
characters.

mv would be your best bet.  But if you HAVE to have a strange name, try
using the __import__() function.



-- 

DaveA




More information about the Python-list mailing list