Skip to content

Commit 5d32ad4

Browse files
committed
section on bytecode
1 parent cc3765d commit 5d32ad4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/writing/gotchas.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,58 @@ When the Gotcha Isn't a Gotcha
184184
Sometimes you want your closures to behave this way. Late binding is good in
185185
lots of situations. Looping to create unique functions is unfortunately a case
186186
where they can cause hiccups.
187+
188+
189+
190+
Bytecode (.pyc) Files Everywhere
191+
--------------------------------
192+
193+
By default, when executing Python code from files, the Python interpreter
194+
will automatically write a bytecode version of that file to disk, e.g.
195+
``module.pyc``.
196+
197+
These ``.pyc`` files should not be checked into your source code repositories.
198+
199+
Theoretically, this behavior is on by default, for performance reasons.
200+
Without these bytecode files present, Python would re-generate the bytecode
201+
every time the file is loaded.
202+
203+
204+
Disabling Bytecode (.pyc) Files
205+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206+
207+
Luckily, the process of generating the bytecode is extremely fast, and isn't
208+
something you need to worry about while developing your code.
209+
210+
Those files are annoying, so let's get rid of them!
211+
212+
::
213+
214+
$ export PYTHONDONTWRITEBYTECODE=1
215+
216+
With the ``$PYTHONDONTWRITEBYTECODE`` environment variable set, Python will
217+
no longer write these files to disk, and your development environment will
218+
remain nice and clean.
219+
220+
I recommend setting this environment variable in your ``~/.profile``.
221+
222+
Removing Bytecode (.pyc) Files
223+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224+
225+
Here's nice trick for removing all of these files, if they already exist::
226+
227+
$ find . -name "*.pyc" -exec rm -rf {} \;
228+
229+
Run that from the root directory of your project, and all ``.pyc`` files
230+
will suddenly vanish. Much better.
231+
232+
233+
234+
235+
236+
237+
238+
239+
240+
241+

0 commit comments

Comments
 (0)