Importing a module without creating a .pyc/.pyo file?
Alex Martelli
aleax at aleax.it
Fri Jan 25 03:18:08 EST 2002
"Thomas Heller" <thomas.heller at ion-tof.com> wrote in message
news:a2r3a9$134nol$1 at ID-59885.news.dfncis.de...
> Is it possible to import a module xxx.py without
> compiling it (implicitely) to .pyo/.pyc, or do I
> always have to delete the compiled file afterwards
> if it gets in the way?
The imp module (and thus no doubt other means yet)
does allow what you're after, e.g.:
import imp
fileob, path, desc = imp.find_module('noc',['.'])
try: noc = imp.load_module('noc', fileob, '', desc)
finally: fileob.close()
print noc.foo
Running this script imports module noc from file
noc.py in the current directory, but does not write
the compiled .pyo/.pyc file (thanks to the '' as
the 3rd, 'filename' argument to load_module). In
this case there's an unpleasant side effect, e.g.
assume there's a syntax error in noc.py:
C:\Python22>python inoc.py
Traceback (most recent call last):
File "inoc.py", line 4, in ?
try: noc = imp.load_module('noc', fileob, '', desc)
File "<string>", line 1
foo=
^
SyntaxError: invalid syntax
...the error is identified as being in '<string>'.
I suspect this could also be worked around...
Alex
More information about the Python-list
mailing list