Created
August 27, 2015 15:53
-
-
Save EricsonWillians/d5dea2f68c9edded170f to your computer and use it in GitHub Desktop.
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.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# PyWinPath.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. | |
# | |
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__": | |
win = Win32Environment('system') | |
PATH = os.environ["PATH"].split(';') | |
search_for = "python.exe" | |
list_of_python_software = ["eclipse", "gimp", "blender", "pycharm", "mysql"] | |
if "python" and "anaconda" not in os.environ["PATH"].lower(): | |
for root, dirs, files in os.walk("C:\\"): | |
try: | |
print("Searching for python.exe in " + root) | |
if search_for in files and not [True for software in list_of_python_software if software in root.lower()][0]: | |
pass | |
except Exception as e: | |
if isinstance(e, UnicodeEncodeError): | |
pass | |
elif isinstance(e, IndexError): | |
print("Found at " + os.path.join(root, search_for)) | |
PATH.append(root) | |
win.setenv("PATH", ';'.join(PATH) + ';') | |
print("The path was properly added to the system's PATH environment variable.") | |
print("Now, just type 'python' in cmd and have fun.") | |
break | |
else: | |
print("You already have python in your system's PATH environment variable.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Em vez de
Que tal:
Fica mais claro dessa forma.