Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to import module whose filename starts number

Reply
Thread Tools

How to import module whose filename starts number

 
 
Yong Hu
Guest
Posts: n/a
 
      12-12-2012
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?
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      12-12-2012
On Dec 12, 2012 9:47 AM, "Yong Hu" <(E-Mail Removed)> 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?
> --


I believe the restriction is that the module names must be valid
identifiers. You may still be able to import them using __import__ and then
assign the resulting module object to a valid name.
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-12-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

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-12-2012
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?


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
RE: Possible to import a module whose name is contained in avariable? Steven Reddie Python 2 03-07-2005 11:32 PM
Possible to import a module whose name is contained in a variable? Steven Reddie Python 10 03-07-2005 02:39 PM
Instant Messenger starts when Outlook Express 6 starts no matter what is checked! Route9w Computer Support 11 07-08-2004 01:13 PM
XSLT Select nodes without text-node children whose names starts with specifix text Michael Reiche XML 3 02-05-2004 10:40 PM



Advertisments