473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

.pyc files??

Say a filea.py contains a number of functions defined in it (using
def afn(): etc.)

in pyhton, filea is imported to use afn (filea.afn() etc.), it cerates
a filea.pyc file, what is the purpose of this file?

-ishwar
Jul 18 '05 #1
4 4086
Ishwar Rattan wrote:
Say a filea.py contains a number of functions defined in it (using
def afn(): etc.)

in pyhton, filea is imported to use afn (filea.afn() etc.), it cerates
a filea.pyc file, what is the purpose of this file?

-ishwar

This file contains compiled bytecode that the Python interpreter uses.
If, in your example, filea.pyc exists and its timestamp is newer than
filea.py, then the interpreter uses filea.pyc without even attempting to
recompile it (which saves the time spent compiling in subsequent runs).
If filea.pyc doesn't exist, or its timestamp is older than filea.py, the
Python interpreter compiles filea.py to bytecode and writes it to
filea.pyc (if it has permissions to do so).

Jul 18 '05 #2
Jeremy Jones wrote:
This file contains compiled bytecode that the Python interpreter uses.
If, in your example, filea.pyc exists and its timestamp is newer than
filea.py, then the interpreter uses filea.pyc without even attempting to
recompile it (which saves the time spent compiling in subsequent runs).
If filea.pyc doesn't exist, or its timestamp is older than filea.py, the
Python interpreter compiles filea.py to bytecode and writes it to
filea.pyc (if it has permissions to do so).


Minor nit-picking: I'm fairly sure that the interpreter just compares
the timestamp for equality, not for "earlier than". I could be
wrong, but I think it's a case of "if the .pyc embedded timestamp
doesn't match the timestamp of the corresponding .py file exactly,
then recompile the .pyc file".

And a quick test at the command line appears to confirm this...

-Peter
Jul 18 '05 #3
Peter Hansen wrote:
Jeremy Jones wrote:
This file contains compiled bytecode that the Python interpreter
uses. If, in your example, filea.pyc exists and its timestamp is
newer than filea.py, then the interpreter uses filea.pyc without even
attempting to recompile it (which saves the time spent compiling in
subsequent runs). If filea.pyc doesn't exist, or its timestamp is
older than filea.py, the Python interpreter compiles filea.py to
bytecode and writes it to filea.pyc (if it has permissions to do so).

Minor nit-picking: I'm fairly sure that the interpreter just compares
the timestamp for equality, not for "earlier than". I could be
wrong, but I think it's a case of "if the .pyc embedded timestamp
doesn't match the timestamp of the corresponding .py file exactly,
then recompile the .pyc file".

And a quick test at the command line appears to confirm this...

Really? All my .pyc files are timestamped later than the .py file that
they were generated from, but they don't seem to be recreated when I
import the module...

[ach]$ ls -l *.py*
-rwxr-xr-x 1 pick pick 1709 Aug 3 18:36 http_multipart.py
-rw-r--r-- 1 pick pick 2437 Aug 4 00:12 http_multipart.pyc
[ach]$ python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import http_multipart

[ach]$ ls -l *.py*
-rwxr-xr-x 1 pick pick 1709 Aug 3 18:36 http_multipart.py
-rw-r--r-- 1 pick pick 2437 Aug 4 00:12 http_multipart.pyc
[ach]$

Jeff Shannon
Technician/Programmer
Credit International
Jul 18 '05 #4
[on when .pyc files are regenerated from .py files]

The .pyc file contains the modification timestamp
in the file itself. It's this embedded timestamp
which is used to check if the .py needs to be rebuilt,
and not a comparison of the respective timestamps
for the file. (Surprised me, I also thought it was the
latter.)

The code is in dist/src/Python/import.c. See
check_compiled_module, which checks the first long
as the magic number then the second long for the
timestamp to use to compare with the mtime of the
..py file. Here's a way to test it:

# Create a simple Python file

% rm spam.py*
% cat > spam.py
1
% python
Python 2.4a2 (#1, Aug 29 2004, 22:30:12)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import spam
% ls -l spam.py*
-rw-r--r-- 1 dalke staff 2 2 Sep 15:23 spam.py
-rw-r--r-- 1 dalke staff 96 2 Sep 15:23 spam.pyc
%
# Let's gets some information about the .pyc

% sum spam.pyc
16387 1 spam.pyc
% od -c spam.pyc
0000000 c 362 \r \n ? 217 7 A c \0 \0 \0 \0 \0 \0 \0
0000020 \0 001 \0 \0 \0 @ \0 \0 \0 s \b \0 \0 \0 d \0
0000040 \0 001 d 001 \0 S ( 002 \0 \0 \0 i 001 \0 \0 \0
0000060 N ( \0 \0 \0 \0 ( \0 \0 \0 \0 ( \0 \0 \0 \0
0000100 ( \0 \0 \0 \0 t 007 \0 \0 \0 s p a m . p
0000120 y t 001 \0 \0 \0 ? 001 \0 \0 \0 s \0 \0 \0 \0
0000140
%

# modify the mtime for the .py file

% touch spam.py
% python
Python 2.4a2 (#1, Aug 29 2004, 22:30:12)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information. import spam
% ls -l spam.py*
-rw-r--r-- 1 dalke staff 2 2 Sep 15:23 spam.py
-rw-r--r-- 1 dalke staff 96 2 Sep 15:23 spam.pyc
%

# any changes? Looks like it.

% sum spam.pyc
21410 1 spam.pyc
% od -c spam.pyc
0000000 c 362 \r \n \ 217 7 A c \0 \0 \0 \0 \0 \0 \0
0000020 \0 001 \0 \0 \0 @ \0 \0 \0 s \b \0 \0 \0 d \0
0000040 \0 001 d 001 \0 S ( 002 \0 \0 \0 i 001 \0 \0 \0
0000060 N ( \0 \0 \0 \0 ( \0 \0 \0 \0 ( \0 \0 \0 \0
0000100 ( \0 \0 \0 \0 t 007 \0 \0 \0 s p a m . p
0000120 y t 001 \0 \0 \0 ? 001 \0 \0 \0 s \0 \0 \0 \0
0000140
%

# the difference was the '?' turned to a '\' in the 5th byte.
# That probably indicates a difference of 29 seconds.
# Now modify the .pyc's mtime

% touch 01010101 spam.pyc
% ls -l spam.py*
-rw-r--r-- 1 dalke staff 2 2 Sep 15:23 spam.py
-rw-r--r-- 1 dalke staff 96 1 Jan 2004 spam.pyc
% python
Python 2.4a2 (#1, Aug 29 2004, 22:30:12)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information. import spam
%

# Notice, no change to the .pyc file. Its mtime is ignored
# and the .pyc is not rebuilt.

% ls -l spam.py*
-rw-r--r-- 1 dalke staff 2 2 Sep 15:23 spam.py
-rw-r--r-- 1 dalke staff 96 1 Jan 2004 spam.pyc
% sum spam.pyc
21410 1 spam.pyc
%

# Another way to see it is with the -v flag to Python.
# Again, the mtime for the .pyc is << that of the .py file.
# See that the .pyc is used without a recompile.

% python -v
[... many lines removed ...]
Python 2.4a2 (#1, Aug 29 2004, 22:30:12)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import readline # dynamically loaded from
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/lib-dynload/readline.so import spam # spam.pyc matches spam.py
import spam # precompiled from spam.pyc [... many lines removed ...]
%

# Now tweak the mtime of the .py file and see that
# the .pyc is regenerated.

% touch spam.py
% python -v
[... many lines removed ...]
Python 2.4a2 (#1, Aug 29 2004, 22:30:12)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import readline # dynamically loaded from
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/lib-dynload/readline.so import spam # spam.pyc has bad mtime
import spam # from spam.py
# wrote spam.pyc

[... many lines removed ...]
Andrew
da***@dalkescientific.com
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
18
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now...
3
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by...
11
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
18
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
0
by: wal | last post by:
How does one attach files to emails using libgmail? The following code http://pramode.net/articles/lfy/fuse/4.txt works fine when said files are simple text files, but it failes as soon as the...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.