[Tutor] Compiled Python File

Steven D'Aprano steve at pearwood.info
Thu Feb 23 05:02:49 CET 2012


On Wed, Feb 22, 2012 at 07:29:42PM -0800, Michael Lewis wrote:
> First, thanks to everyone who helped me out regarding my Recognizing real
> numbers post. I gained a lot of knowledge over the past two days!
> 
> I've noticed that after I execute some of my .py files, a Compiled Python
> File is automatically saved to my directory. This doesn't happen for every
> file that I execute though. Why is that?

When you execute a file directly from the shell, using something like 
this command:

$ python my_python_script.py

no .pyc file is created. But when you import a file from within Python, 
either by hand or within a script:

>>> import my_module

Python compiles it and saves the compiled version to my_module.pyc so 
that the next time you call import, it will be faster.


-- 
Steven


More information about the Tutor mailing list