It’s not our fault, it’s not ActiveState’s fault…it’s Microsoft’s fault (sorry, guys). But don’t be scared, because with a little bit of elbow grease and some Python magic, we can fix this ***** bug once and for all!
So what’s the issue? Well, if you’re using ActiveState 2.6 on Windows, you might have noticed that your .pyc files are getting corrupted. This is because Microsoft decided to be a little too helpful when it comes to file permissions (or lack thereof). They set the default permission for new files to “System” and “Administrator,” which means that regular users can’t write to them.
Now, you might think this isn’t a big deal…until you try to run your Python scripts. When Python tries to compile your .py file into a .pyc file (which it does automatically), it needs to be able to write to the .pyc file. But since regular users can’t write to them, Python gets all confused and throws an error.
This is where we come in! We’re going to fix this bug by modifying the way that Python creates new files on Windows. Instead of relying on Microsoft’s default permissions, we’ll set the permission ourselves so that regular users can write to them. Here’s how:
1. Open up your favorite text editor (or IDE) and navigate to the “sitecustomize.py” file in your Python installation directory. This is where we’re going to add our fix.
2. Add the following code at the bottom of the file:
# Import necessary modules
import os, stat # Import the os and stat modules for file and directory operations
from distutils import sysconfig # Import the sysconfig module for system configuration
# Define a function to set file permissions
def _set_file_permissions(filename):
mode = 0o644 # Set read/write permissions for owner and group
try:
os.chmod(filename, mode) # Use os.chmod() to change file permissions
except OSError:
pass # If an error occurs, do nothing
# Add this function to the list of functions that are called when Python starts up
sysconfig.set_pythonhome() # Set the Python home directory
import sys # Import the sys module for system-specific parameters and functions
from pydoc import doc # Import the doc function from the pydoc module
doc('sitecustomize') # Use the doc function to display documentation for the sitecustomize module
# Define a function to customize the site settings
def sitecustomize():
# Call our new function whenever a .pyc file is created
for dirname, _, filenames in os.walk(sys.prefix + '/lib/site-packages'): # Use os.walk() to traverse through directories
if not (dirname.startswith(sys.prefix) and dirname.endswith('__pycache__')): # Check if the directory is within the Python installation directory
continue # If not, skip to the next directory
for filename in filenames:
if filename.endswith('.pyc') and os.path.exists(os.path.join(dirname, filename)): # Check if the file is a .pyc file and if it exists
_set_file_permissions(filename) # Call the _set_file_permissions function to set file permissions
# Call the sitecustomize function
sitecustomize()
3. Save the file and close your text editor/IDE.
4. Restart Python (or open a new terminal window if you’re using IPython).
5. Test it out by running one of your Python scripts that generates .pyc files. You should now be able to write to those .pyc files without any issues!
And there you have it, A simple fix for a Windows-only issue in ActiveState 2.6 builds. It’s not perfect (we still need to figure out how to make this work on other platforms), but it’s better than nothing. So let’s give Microsoft a round of applause…for being the reason we can’t have nice things!
Later!