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


Groups > comp.lang.python > #71905

Re: All-numeric script names and import

Date 2014-05-22 12:32 +0200
From Xavier de Gaye <xdegaye@gmail.com>
Subject Re: All-numeric script names and import
References <CAPTjJmoadqpS0O-15We7xi7SzFCVxTFe1TU8hConcGZzxTRdcA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.10228.1400780259.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 05/21/2014 03:46 PM, Chris Angelico wrote:
 > If I have a file called 1.py, is there a way to import it? Obviously I
 > can't import it as itself, but in theory, it should be possible to
 > import something from it. I can manage it with __import__ (this is
 > Python 2.7 I'm working on, at least for the moment), but not with the
 > statement form.
 >
 > # from 1 import app as application # Doesn't work with a numeric name
 > application = __import__("1").app
 >
 > Is there a way to tell Python that, syntactically, this thing that
 > looks like a number is really a name? Or am I just being dumb?
 >
 > (Don't hold back on that last question. "Yes" is a perfectly
 > acceptable answer. But please explain which of the several
 > possibilities is the way I'm being dumb. Thanks!)
 >
 > ChrisA
 >

import 1.py as module_1 on Python 2.7 (module_1 is not inserted in sys.modules):

 >>> import imp
 >>> module_1 = imp.new_module('module_1')
 >>> execfile('1.py', module_1.__dict__)
 >>> del module_1.__dict__['__builtins__']

Xavier

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


Thread

Re: All-numeric script names and import Xavier de Gaye <xdegaye@gmail.com> - 2014-05-22 12:32 +0200

csiph-web