Newbie createing .PYC for import

David Bolen db3l at
Thu Mar 1 19:57:28 CET 2001

"Larry Bates" <lbates at> writes:

> I have spent the last couple of hours (unsuccessfully) attempting to figure
> out how to generate a .PYC file for a module that I've defined.  Everything
> appears to imply that it's "automatic", but I'm not getting anywhere.  I
> have a module and I wish to generate a .PYC file so that I can import it
> into future projects.  Can anyone shed any light on how to accomplish this?

It's "automatic" if you are importing the module and Python has the
ability (permissions, free space, etc...) to write the compiled module
back to the directory.

Note that running Python on a top level script is not considered an
import and thus no pyc will be created by default.

You can manually compile any module using the py_compile module, and,
for example, the compile() function in that module, so interactively:

    >>> import py_compile
    >>> py_compile.compile('filename')

and this will write the pyc to the same location as filename (or you
can override that with the optional parameter cfile).

You can also automatically compile all files in a directory or
directories using the compileall module, which can also be run
straight from the command line.

-- David
 \               David Bolen            \   E-mail: db3l at  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list