Hey everyone! I'm looking to generate bytecode (.pyc files) for a class called Foo in Python. I've also got another class, Foo2, that inherits from Foo. My idea is to share the Foo2 source code along with the bytecode for Foo so that my students can use the methods from Foo2 without actually seeing the implementation of Foo. This would help them focus on certain tasks while comparing their work with the parent class methods. The challenge I'm facing is the need to create bytecode compatible with various Python versions. Does anyone know a good way to generate the bytecode for different Python versions? Or do you have any alternative suggestions? I've included a simple example of my code: [https://godbolt.org/z/WdcWsvo4c](https://godbolt.org/z/WdcWsvo4c)
4 Answers
Just a thought—obfuscated code can be pretty tricky to work with. It might complicate things more than help them understand the basics.
If you really need to go this route, consider rewriting the main library in C or using Cython 3.1+. This way, you can compile one library per platform and keep compatibility across Python versions.
You might want to check out PyArmor. It's a handy tool for obfuscating your Python scripts, and it could work well for your scenario! It allows you to distribute your Foo2 class without revealing Foo's code, while still working with various Python versions. You can find it on GitHub: https://github.com/dashingsoft/pyarmor.
Just a heads up though, PyArmor has similar restrictions as bytecode itself—it means that if you obfuscate code for Python 3.8, it’ll only run on 3.8.x and won't work on earlier or later versions.
Honestly, why not just tell your students to treat foo.py as a third-party library? For this lesson, they should only modify foo2.py and stick to the specs. It’s also a learning experience to follow instructions!
Interesting, I didn't know that! I'll definitely take a look!