Refactoring "from _ import *" to "import _ as _"
Answered
I have a large tkinter GUI that I'm refactoring. When I created it, I used "from tkinter import * ", which is prevalent, but wrong. I want to use "import tkinter as tk" instead, but adding "tk." in front of all of the tkinter names is onerous. For example, keyword arguments such as "Y", "YES", "NO", "NONE", "W", "E" etc. all need to be renamed "tk.Y", "tk.YES" etc., without corrupting other names (e.g. not change YearlyEarnings() to tk.Yearlytk.Earnings() ).
I haven't found a way to do so in PyCharm yet, but it's so feature-rich I suspect that this is possible. Any pointers in the right direction would be greatly appreciated!
Please sign in to leave a comment.
Hi there,
If you hit Alt+Enter (Show Intention Actions) on that "from tkinter import *" import there is the intention "Convert to 'import tkinter'". Then, after "from" import is converted to a plain one, there will be another suggestion "Add alias to tkinter", and that does what you want.
Thank you @... for the useful question and Mikhail Golubev for the great working and simple answer!