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


Groups > comp.lang.python > #34713

Re: How to import module whose filename starts number

Date 2012-12-12 12:53 -0500
From Dave Angel <d@davea.name>
Subject Re: How to import module whose filename starts number
References <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.789.1355334831.29569.python-list@python.org> (permalink)

Show all headers | View raw


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

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