Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34707 > unrolled thread

How to import module whose filename starts number

Started byYong Hu <yhu221300@gmail.com>
First post2012-12-12 09:42 -0800
Last post2012-12-12 09:50 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How to import module whose filename starts number Yong Hu <yhu221300@gmail.com> - 2012-12-12 09:42 -0800
    Re: How to import module whose filename starts number Dave Angel <d@davea.name> - 2012-12-12 12:53 -0500
    Re: How to import module whose filename starts number Peter Otten <__peter__@web.de> - 2012-12-12 18:57 +0100
    Re: How to import module whose filename starts number Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-12-12 09:50 -0800

#34707 — How to import module whose filename starts number

FromYong Hu <yhu221300@gmail.com>
Date2012-12-12 09:42 -0800
SubjectHow to import module whose filename starts number
Message-ID<39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com>
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?

[toc] | [next] | [standalone]


#34713

FromDave Angel <d@davea.name>
Date2012-12-12 12:53 -0500
Message-ID<mailman.789.1355334831.29569.python-list@python.org>
In reply to#34707
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

[toc] | [prev] | [next] | [standalone]


#34714

FromPeter Otten <__peter__@web.de>
Date2012-12-12 18:57 +0100
Message-ID<mailman.790.1355334990.29569.python-list@python.org>
In reply to#34707
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.

[toc] | [prev] | [next] | [standalone]


#34715

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2012-12-12 09:50 -0800
Message-ID<mailman.791.1355335029.29569.python-list@python.org>
In reply to#34707

[Multipart message — attachments visible in raw view] — view raw

On Dec 12, 2012 9:47 AM, "Yong Hu" <yhu221300@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web