Skip to content

Instantly share code, notes, and snippets.

@rafguns
Created August 3, 2012 07:27
Show Gist options
  • Save rafguns/3245380 to your computer and use it in GitHub Desktop.
Save rafguns/3245380 to your computer and use it in GitHub Desktop.
Fix Python-based commands/scripts for Python 2.7 on Windows 7 (x64)
"""
Fix Python-based commands/scripts for Python 2.7 on Windows 7 (x64)
Due to Windows 7 UAC requirements Python-based commands (such as easy_install,
ipython, nosetests, pip...) open a new terminal window, making it hard to read
the output.
As documented elsewhere [1]_, the most practical solution is apparently replacing
the executables with a batch file that calls the Python script. This is tedious to
do by hand if you have many packages that install new commands.
This script automates the process: it moves the executables (e.g., pip.exe) and
their manifest files (e.g., pip.exe.manifest) to a separate directory, and replaces
them with a batch file with the same name (e.g., pip.bat). It should be run without
arguments::
>python exe2bat.py
.. _[1] http://stackoverflow.com/questions/5814759/python-packages-open-new-window
"""
import os
scriptsdir = "c:/python27/scripts"
exedir = os.path.join(scriptsdir, "EXEs")
if not os.path.exists(exedir):
os.mkdir(exedir)
os.chdir(scriptsdir)
fnames = os.listdir('.')
for fname in fnames:
if fname.lower().endswith(".exe"):
# Do we have a corresponding script?
basename = os.path.splitext(fname)[0]
script = basename + "-script.py"
if script not in fnames:
raise Exception("Expected script '%s' could not be found." % script)
# Move EXE and corresponding manifest to exedir
os.rename(fname, os.path.join(exedir, fname))
manifest = fname + ".manifest"
os.rename(manifest, os.path.join(exedir, manifest))
# Make BAT file
with open(basename + ".bat", "w") as f:
f.write("@echo off\n")
f.write("python %s %%*" % os.path.join(scriptsdir, script))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment