[Tutor] Sample9.py and Sample9.pyc & seting path to Python open files ...

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 25 14:45:47 EST 2003

On Tue, 25 Nov 2003, David wrote:

> 1.) I noticed, that after I have imported some random module (in this
> case, module I made) there appear an additional file in Python\Saves
> directory, where my modules are stored ...
> The module I imported was Sample9.py (green icon), process it. and just
> after last line the new file Sample9.pyc appeared (more or less red
> icon) ...
> It has also different content compare to "original" file, some sort of
> "program language" I do not recognize ...

Hi Tadey,

Yes.  That second file that you're seeing, with the '.pyc' extention, is a
Python "bytecode-compiled" file.  Before Python starts to run a program,
it takes a step to read the whole program, digest it, and turn it into an
internal "bytecode" format that's simpler for it to understand.  Think of
a cow that ruminates: Python does something similar to rumination.

For example, the relatively simple function:

>>> def hello():
...     print "Hello world!"

is processed by Python, and digested into a set of relatively simple
"bytecode" instructions:

>>> import dis
>>> dis.dis(hello)
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_CONST               1 ('Hello world!')
          9 PRINT_ITEM
         10 PRINT_NEWLINE
         11 LOAD_CONST               0 (None)
         14 RETURN_VALUE

The 'bytecodes' here are very primitive instructions, but they capture
everything that our original hello() function is doing.

Usually, Python keeps these bytecodes only for as long as a program is
running.  But since it actually takes a bit of work for Python to convert
our text program into primitive bytecodes, sometimes Python will try to
save the bytecodes to disk, if it feels it'll be useful in the future.

For example, it'll write the bytecodes out to disk if we do a module
'import': module libraries are heavily used, so it makes sense to save the
.pyc file to disk to save some work.

You never have to explictely tell Python to byte-compile a Python file
though.  It also shouldn't hurt if you delete a .pyc file, because it's
simply a processed version of your '.py' files.  But in general, you
shouldn't need to worry too much about them.

> 2.) Just before writting this mail, when I was trying to make some more
> complex "read/write" file operatons, I always get an error:
> IOError [Errno 2] No such file or directory: 'Sample.py'

What kind of reading and writing operation are you doing?  Show us the
code, and we can take a look.  In particular, it'll help if we see the
line that the error message is pointing at.  Are you trying to write to a
text file?  And if so, why 'Sample.py'?

The list 'sys.path' does not control where a file is to be opened: it does
not change the present working directory.  Instead, it's meant to tell
Python a bunch of locations where it can find modules to 'import'.

It shouldn't be involved with any file 'open()'ing that you're doing,
though, so I'm not sure if you should be using sys.path.  I think we'll
need to see more of your program, though, before saying more.

I actually have to go at the moment, but the other tutors may handle your
third question about logical operators.  Talk to you later!

More information about the Tutor mailing list