def _check_upgrade_private_venv(self, path): # If home is wrong then regenerate # If only micro version is different, then upgrade info = _get_venv_info(path) if not is_same_path(info["home"], os.path.dirname(sys.executable)): self._create_private_venv( path, "Thonny's virtual environment was created for another interpreter.\n" + "Regenerating the virtual environment for current interpreter.\n" + "(You may need to reinstall your 3rd party packages)\n" + "Please wait!.", clear=True, ) else: venv_version = tuple(map(int, info["version"].split("."))) sys_version = sys.version_info[:3] assert venv_version[0] == sys_version[0] assert venv_version[1] == sys_version[1] if venv_version[2] != sys_version[2]: self._create_private_venv( path, "Please wait!\nUpgrading Thonny's virtual environment.", upgrade=True)
def _create_widgets(self, parent): bg = "#ffff99" banner = tk.Label(parent, background=bg) banner.grid(row=0, column=0, sticky="nsew") banner_msg = ( "This dialog is for managing Thonny plug-ins and their dependencies.\n" + "If you want to install packages for your own programs then choose 'Tools → Manage packages...'\n" ) runner = get_runner() if (runner is not None and runner.get_local_executable() is not None and is_same_path(self._get_interpreter(), get_runner().get_local_executable())): banner_msg += "(In this case Thonny's back-end uses same interpreter, so both dialogs manage same packages.)\n" banner_msg += ( "\n" + "NB! You need to restart Thonny after installing / upgrading / uninstalling a plug-in." ) banner_text = tk.Label(banner, text=banner_msg, background=bg, justify="left") banner_text.grid(pady=10, padx=10) PipDialog._create_widgets(self, parent)
def file_is_opened(self, path): for editor in self.get_all_editors(): if editor.get_filename() and is_same_path(path, editor.get_filename()): return True return False
def get_editor(self, filename, open_when_necessary=False): for child in self.winfo_children(): child_filename = child.get_filename(False) if child_filename and is_same_path(child.get_filename(), filename): return child if open_when_necessary: return self._open_file(filename) else: return None
def _get_warnings(self, main_file_path): # TODO: current dir may be different main_file_dir = os.path.dirname(main_file_path) if not os.path.isdir(main_file_dir): return [] library_modules = known_stdlib_modules | self._get_3rd_party_modules() for item in os.listdir(main_file_dir): full_path = os.path.join(main_file_dir, item) if item.endswith(".py") and item[:-3] in library_modules: if is_same_path(full_path, main_file_path): prelude = "Your program file is named '%s'." % item rename_hint = " (*Run → Rename…* )" else: prelude = ( "Your working directory `%s <%s>`__ contains a file named '%s'.\n\n" % ( rst_utils.escape(main_file_dir), rst_utils.escape(main_file_dir), item, )) rename_hint = "" yield { "filename": full_path, "lineno": 0, "symbol": "file-shadows-library-module", "msg": "Possibly bad file name", "explanation_rst": prelude + "\n\n" + "When you try to import library module ``%s``, your file will be imported instead.\n\n" % item[:-3] + "Rename your '%s'%s to make the library module visible again." % (item, rename_hint), "group": "warnings", "relevance": 5, }
def get_environment_overrides_for_python_subprocess(target_executable): """Take care of not not confusing different interpreter with variables meant for bundled interpreter""" # At the moment I'm tweaking the environment only if current # exe is bundled for Thonny. # In remaining cases it is user's responsibility to avoid # calling Thonny with environment which may be confusing for # different Pythons called in a subprocess. this_executable = sys.executable.replace("pythonw.exe", "python.exe") target_executable = target_executable.replace("pythonw.exe", "python.exe") interpreter_specific_keys = [ "TCL_LIBRARY", "TK_LIBRARY", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH", "SSL_CERT_DIR", "SSL_CERT_FILE", "PYTHONHOME", "PYTHONPATH", "PYTHONNOUSERSITE", "PYTHONUSERBASE", ] result = {} if os.path.samefile( target_executable, this_executable ) or is_venv_interpreter_of_current_interpreter(target_executable): # bring out some important variables so that they can # be explicitly set in macOS Terminal # (If they are set then it's most likely because current exe is in Thonny bundle) for key in interpreter_specific_keys: if key in os.environ: result[key] = os.environ[key] # never pass some variables to different interpreter # (even if it's venv or symlink to current one) if not is_same_path(target_executable, this_executable): for key in [ "PYTHONPATH", "PYTHONHOME", "PYTHONNOUSERSITE", "PYTHONUSERBASE" ]: if key in os.environ: result[key] = None else: # interpreters are not related # interpreter specific keys most likely would confuse other interpreter for key in interpreter_specific_keys: if key in os.environ: result[key] = None # some keys should be never passed for key in [ "PYTHONSTARTUP", "PYTHONBREAKPOINT", "PYTHONDEBUG", "PYTHONNOUSERSITE", "PYTHONASYNCIODEBUG", ]: if key in os.environ: result[key] = None # venv may not find (correct) Tk without assistance (eg. in Ubuntu) if is_venv_interpreter_of_current_interpreter(target_executable): try: if "TCL_LIBRARY" not in os.environ or "TK_LIBRARY" not in os.environ: result["TCL_LIBRARY"] = get_workbench().tk.exprstring( "$tcl_library") result["TK_LIBRARY"] = get_workbench().tk.exprstring( "$tk_library") except Exception: logging.exception("Can't compute Tcl/Tk library location") return result