Can I run the python interpreter without generating the compiled .pyc files?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You could make the directories that your modules exist in read-only for the user that the Python interpreter is running as.
I don't think there's a more elegant option. PEP 304 appears to have been an attempt to introduce a simple option for this, but it appears to have been abandoned.
I imagine there's probably some other problem you're trying to solve, for which disabling .py[co] would appear to be a workaround, but it'll probably be better to attack whatever this original problem is instead.
From "What’s New in Python 2.6 - Interpreter Changes":
Update 2010-11-27: Python 3.2 addresses the issue of cluttering source folders with
.pyc
files by introducing a special__pycache__
subfolder, see What's New in Python 3.2 - PYC Repository Directories.There actually IS a way to do it in Python 2.3+, but it's a bit esoteric. I don't know if you realize this, but you can do the following:
According to the zipimport library:
Thus, all you have to do is zip the files up, add the zipfile to your sys.path and then import them.
If you're building this for UNIX, you might also consider packaging your script using this recipe: unix zip executable, but note that you might have to tweak this if you plan on using stdin or reading anything from sys.args (it CAN be done without too much trouble).
In my experience performance doesn't suffer too much because of this, but you should think twice before importing any very large modules this way.