fpm icon indicating copy to clipboard operation
fpm copied to clipboard

Feature: flag to disable Python bytecode generation (pyc and __pycache__ files)

Open jchadwick-smug opened this issue 4 years ago • 3 comments

At my workplace we often build Python packages using fpm -s python -t deb.

However, this has the negative side effect of the spawned setup.py and subsequent bits generating .pyc files and __pycache__ directories, which are not supposed to be part of Debian/Ubuntu packages. There are several reasons for this which are irrelevant to this discussion, but here's some historic evidence:

  • https://blog.mozilla.org/webdev/2015/10/27/eradicating-those-nasty-pyc-files/
  • https://bugs.debian.org/901637

(This is in direct conflict with #468 where the requester actually desired pyc/pycache bits. Just goes to show everyone's use-cases are different; we try to comply with Debian/Ubuntu distro packaging norms.)

There are 2 clean ways in Python to cease this behaviour. This applies to both Python 2 and Python 3 (even extremely old versions), but we use Python 3 in the below example:

  1. By setting the PYTHONDONTWRITEBYTECODE environment variable in some manner (ex. export PYTHONDONTWRITEBYTECODE=1, or PYTHONDONTWRITEBYTECODE=1 python3 setup.py ...), or,
  2. Running Python with the -B flag

I couldn't get fpm to let me do either. Reasons:

  • fpm does not inherit the parent environment; it uses execl() directly for /usr/bin/python3 rather than system(). Shell env vars, /etc/environment, etc. all have no bearing,
  • fpm does not let you do something like --python-bin '/usr/bin/python3 -B' to try and force the -B flag into the argument list (the argument itself is a literal string)

The current model fpm uses for launching setup.py is perfectly sound and reasonable (using system() opens up a very ugly can of worms); we don't want to see that part changed. But...

We'd benefit greatly from a flag, like --python-no-bytecode (call it whatever you want), which would either:

  1. Use execle() to include the PYTHONDONTWRITEBYTECODE environment variable, or,
  2. Executing python3 (or --python-bin) with the -B flag when running setup.py. (I suspect this option is safer and very easy to implement)

This would save us from having to "do everything by hand", ending up using fpm -s dir -t deb instead.

Thank you!

jchadwick-smug avatar Mar 26 '20 02:03 jchadwick-smug

Hey, I was just looking for such an option, so "Nice idea" IMHO !

In the meantime, I've just added :

import sys
sys.dont_write_bytecode = True

... to the setup.py, and the resulting .DEB package does not contain any byte-code anymore :ok_hand: :100:

Source : https://stackoverflow.com/a/9562273.

Bye :wave:


EDIT 2020-03-28 : @jchadwick-smug a cleaner method to imitate the "regular" Setuptools behavior.


EDIT 2020-04-14 : Hmmm... It finally appears the environment variable is well propagated in my case, even without the workaround described above :thinking:

HorlogeSkynet avatar Mar 26 '20 16:03 HorlogeSkynet

Thank you :)

On Thu, Mar 26, 2020 at 9:34 AM Samuel FORESTIER [email protected] wrote:

Hey, I was just looking for such an option, so "Nice idea" IMHO !

In the meantime, I've just added :

import sys

sys.dont_write_bytecode = True

... to the setup.py, and the resulting .DEB package does not contain any byte-code anymore 👌 💯

Source : https://stackoverflow.com/a/9562273.

Bye 👋

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jordansissel/fpm/issues/1690#issuecomment-604534037, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABAF2QZI24VRV6DJALCF6TRJN7ZXANCNFSM4LT4V5VQ .

jordansissel avatar Mar 26 '20 16:03 jordansissel

Setting PYTHONDONTWRITEBYTECODE=1 (PYTHONDONTWRITEBYTECODE=1 fpm ...) works for me too. I can't think of a reason to ever want such files in a Debian package. Should fpm invoke Python with -B by default?

WilliamDEdwards avatar Aug 27 '22 09:08 WilliamDEdwards