How to import module whose filename starts number

Discussion in 'Python' started by Yong Hu, Dec 12, 2012.

  1. Yong Hu

    Yong Hu Guest

    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?
     
    Yong Hu, Dec 12, 2012
    #1
    1. Advertising

  2. On Dec 12, 2012 9:47 AM, "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?
    > --


    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
     
    Benjamin Kaplan, Dec 12, 2012
    #2
    1. Advertising

  3. Yong Hu

    Dave Angel Guest

    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
     
    Dave Angel, Dec 12, 2012
    #3
  4. Yong Hu

    Peter Otten Guest

    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.
     
    Peter Otten, Dec 12, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Reiche
    Replies:
    3
    Views:
    10,858
    Michael Reiche
    Feb 5, 2004
  2. plb
    Replies:
    0
    Views:
    400
  3. plb
    Replies:
    0
    Views:
    318
  4. Steven Reddie
    Replies:
    10
    Views:
    579
    Michael Hoffman
    Mar 7, 2005
  5. Steven Reddie
    Replies:
    2
    Views:
    311
    Scott David Daniels
    Mar 7, 2005
Loading...

Share This Page