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 4 4038
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).
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
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
[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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |