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


Groups > comp.lang.python > #34714

Re: How to import module whose filename starts number

From Peter Otten <__peter__@web.de>
Subject Re: How to import module whose filename starts number
Date 2012-12-12 18:57 +0100
Organization None
References <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.790.1355334990.29569.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web