7

Can I remove python source files (*.py) after byte-compiling? I know this does not provide a security and that .pyc/o files can be reverse-engineered very easily; I just want to release less files on a release.

# Clean the python cache
find ./mypythonstuffs -type f -name "*.py[co]" -delete
find ./mypythonstuffs -type d -name "__pycache__" -delete

# Remove test directories
find ./mypythonstuffs -name "test" -type d -exec rm -r "{}" \;

# Byte-compile, ignoring .git subdirectories
python3 -O -m compileall -f -x r'[/\\][.]git' ./mypythonstuffs

# Remove now-unnecessary??? python source files
find ./mypythonstuffs -type f -name "*.py" -delete
4
  • 2
    You can do this but why don't you just use .gitignore? Seems like a lot of work to avoid something that should be trivially easy to leave out of your releases. Commented Nov 2, 2015 at 22:38
  • I ONLY want the pycache directories, NOT the actual source files to be part of the release. Commented Nov 2, 2015 at 22:40
  • 1
    So is your question, "Can I do this in my releases?" Yes, trivially. Is it instead, "Will I still be able to start my program and have people run it without issue?" I personally can't answer that with no code sample, and that's also not what your question says right now. Commented Nov 2, 2015 at 22:45
  • 1
    You may have problems with different python subversions not being able to run the code. Python code scanners looking for doc strings, etc...., may not work which could be annoying for the user's IDE and potentially with test and profiling software. But its a legal thing to do. An alternate solution is to build an official python distributable such as a wheel that works with pip. Commented Nov 2, 2015 at 22:50

2 Answers 2

3

Please do not do this.

First off, python bytecode is an undocumented implementation detail. There is no guarantee that distributing bytecode to other people will actually work.

Second, it makes it hard to debug. You know how tracebacks show the lines of source code? That's taken from the original source files. Even if you don't care about FOSS at all, you should still care about getting helpful bug reports from users.

Third, it is really unnecessary. If you want to distribute less files, just distribute the python source files. Bytecode is an implementation detail. You shouldn't be checking it in or distributing it in the first place.

Sign up to request clarification or add additional context in comments.

1 Comment

"There is no guarantee that distributing bytecode to other people will actually work" — there actually is if you use containers.
-1

Yes, you can deliver an application as just .pyc files.

But if you're just interested in reducing file count, you could deliver the .py files instead of the .pyc ones, right?

1 Comment

The __pycache__ is generated from the .py source so no, we'd have double (approximately) the files in a release with both. If I remove all the .py files, what is the first one to call? Do I need to retain a top-level main?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.