Last active
October 16, 2015 02:06
-
-
Save EricsonWillians/de83b9b3cfac6b45fd43 to your computer and use it in GitHub Desktop.
Pylazy is a script that changes in the PATH windows environment variable from python 2.x to python 3.x or vice-versa.
This file contains hidden or 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
# | |
# pylazy.py | |
# | |
# Copyright 2015 Ericson Willians (Rederick Deathwill) <EricsonWRP@ERICSONWRP-PC> | |
# | |
# This script recursively searches for python.exe in the C partition of the system, | |
# And adds its path to the system's PATH environment variable to help new users. | |
# It also considers some basic common softwares that uses Python, such as GIMP, and ignores them. | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
# | |
# Important Observations: | |
# | |
# I. Be sure to have pywin32 installed for both Python 2.x and Python 3.x. | |
# II. BE SURE TO CHECK WHETHER YOU HAVE "PYTHONPATH" OR "PYTHONHOME" IN YOUR USER ENVIRONMENT VARIABLES OR NOT, | |
# for that could cause some creepy conflict errors... | |
# III. If you're using Geany or any other IDE, be sure to CLOSE it before changing from one version to another again. | |
# IV. Be sure to know what you're doing... | |
# | |
import os, sys | |
from subprocess import check_call | |
if sys.hexversion > 0x03000000: | |
import winreg | |
else: | |
import _winreg as winreg | |
class Win32Environment: | |
""" | |
Brilliant Utility class to get/set windows environment variables using winreg. | |
Got from: http://code.activestate.com/recipes/577621/ | |
""" | |
def __init__(self, scope): | |
assert scope in ('user', 'system') | |
self.scope = scope | |
if scope == 'user': | |
self.root = winreg.HKEY_CURRENT_USER | |
self.subkey = 'Environment' | |
else: | |
self.root = winreg.HKEY_LOCAL_MACHINE | |
self.subkey = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
def getenv(self, name): | |
key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_READ) | |
try: | |
value, _ = winreg.QueryValueEx(key, name) | |
except WindowsError: | |
value = '' | |
return value | |
def setenv(self, name, value): | |
# Note: for 'system' scope, you must run this as Administrator | |
key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS) | |
winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value) | |
winreg.CloseKey(key) | |
# For some strange reason, calling SendMessage from the current process | |
# doesn't propagate environment changes at all. | |
# TODO: handle CalledProcessError (for assert) | |
check_call('''\ | |
"%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable) | |
if __name__ == "__main__": | |
py3_root_path = "G:\\Insanity\\Python\\Anaconda3" # YOUR PATH HERE | |
py3_scripts_path = "G:\\Insanity\\Python\\Anaconda3\\Scripts" # YOUR PATH HERE | |
py2_root_path = "G:\\Insanity\\Python\\Python27" # YOUR PATH HERE | |
py2_scripts_path = "G:\\Insanity\\Python\\Python27\\Scripts" # YOUR PATH HERE | |
win = Win32Environment('system') | |
PATH = os.environ["PATH"].split(';') | |
if sys.version_info[0] == 3: | |
if py3_root_path in PATH and py3_scripts_path in PATH: | |
PATH = [py2_root_path if path == py3_root_path else path for path in PATH] | |
PATH = [py2_scripts_path if path == py3_scripts_path else path for path in PATH] | |
else: | |
exec('print("py3_root_path and py3_scripts_path are not in environment PATH variable.")') | |
elif sys.version_info[0] == 2: | |
if py2_root_path in PATH and py2_scripts_path in PATH: | |
PATH = [py3_root_path if path == py2_root_path else path for path in PATH] | |
PATH = [py3_scripts_path if path == py2_scripts_path else path for path in PATH] | |
else: | |
exec('print "py2_root_path and py2_scripts_path are not in environment PATH variable."') | |
win.setenv("PATH", ';'.join(PATH) + ';') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment