-
-
Save heatherannelynn/ae844defc044907edd47133690325ca5 to your computer and use it in GitHub Desktop.
Fix Python-based commands/scripts for Python 2.7 on Windows 7 (x64)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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