hi serdar here from infoworld at idg today in smart python we're going to look at python's
bytecode caching system which is generated from programs that you run and helps make them run a
little faster when you write a program in python and you feed it to the python runtime it isn't run
as is it's first converted into what's called bytecode a series of instructions used by the
python runtime python code is easier for humans to read but bytecode is easier for the machine
to read and work with so we write in python and we have the python runtime convert to bytecode
to make the job easier for everyone involved in an earlier video which i've linked below i mentioned
using the dis module to show how bytecode instructions compare to the readable python
counterparts now because the conversion process can sometimes take some time python will often
save a copy of the converted byte code as a file alongside the original so when future runs of the
same script it will just read back in the bytecode rather than reconverting it every single time the
bytecode is stored in a binary format using the file extension.pyc and it's kept in a subdirectory
of the directory where the source code lives this subject is called pi cache with double underscores
before and after each of the cache files in the pi cache directory is named after the original file
with an identifier that indicates what version of python it's used with the cache files we
see here are for c python the standard python runtime version 3.8 if we've run this program
with a different edition of python it's going to generate new cache files for that version
now pyc files are functionally interchangeable with pi files in other words i can replace any pi
file with its pyc equivalent and it will behave exactly the same way it just won't be readily
editable it'll also only be possible to run the pyc file on the version of python that it was
originally generated with so if i generated pycs from version 3.8 they will only run on version 3.8
now sometimes you'll find that cache files aren't generated for some scripts for instance in this
project the conway.pi file has no corresponding bytecode file in the cache directory that's
because python has a quirk where it typically doesn't generate bytecode files for whatever is
the entry point in an application and this is to avoid the overhead of creating bytecode files for
something that we're just going to run maybe once so that we're not cluttering up the application
directory with it conway and conway cyton are run directly and so there's no byte code generated for
them but they both import this module timer and so we have a bytecode cache for timer since it
will probably be reused repeatedly on future runs now we can also instruct the python runtime to not
create bytecode at all if we use the dash b flag when running a script we don't generate any pyc
files for that run this is useful if you want to ensure that no cache is ever created for a
particular script and that you're always using the human readable source version as your actual code
now what should you do with cache files and the short answer is generally you don't do anything
with them you should just leave them alone if a given cache file is out of date compared to
its source code python will detect this situation and will regenerate the cache file automatically
also note that you should never place anything else manually in a pi cache folder
they should be considered read-only by you one other thing you should do is exclude
cache directories from any git or source control systems that you're using for your python project
there's no point in committing them since they just get regenerated anyway and they add unneeded
noise and overhead to a project's repository if you're using git you can use a line like this
in your git ignore to ignore all the cache directories in a project now if you're a bit
obsessive compulsive like me and you'd like to perform cleanups on an entire project directory
and get rid of all of the pi cache subdirectories in one swoop you can do this you can recursively
clean out pi c files in the directories from your repo just to get a fresh start here's an example
of a powershell script that will do that you run this from the top of your project directory and
it'll clean out every podcast subfolder the only downside of course is that the next time
you run your application those things will have to be regenerated that's it for this episode if
you liked it please leave a comment below don't forget to subscribe to the idg techtalk channel
on youtube and for more smart python be sure to follow us on facebook youtube and infoworld.com