Refactoring a python project that has a lot libraries that have "from smthng import *"

138 views Asked by At

I start a python project that continues developing. It has 10 local libraries. Each of them call each other "from name import *". For a example from main:

from name3 import *
from name2 import *
from general_functions import *
from messages import *
from network import *
from keyboard_functions import *
from process_functions import *
from name4 import *
from settings import *  # settings

According to zen of python, explicit is better than implicit. Also due to understanding and debugging project, i need explicit the importing methods and variables.

Also other libraries use the same import methods. I feel all libraries import all libraries by the "*".

I try to refactoring by the hand

from debug_functions import (print_log, print_log2, debug_functions_initilizer, debug_prints_abbrev, print_setting_info,
                             print_error, print_warn, print_error_warning_count_of_this_process, print_debug,
                             print_performance_report)

like this.

But i stopped when i see deep.

For example a.py import exp1 library. In the b.py, it call from a.py import *. So in the b.py, exp1 library can be used without import exp1. So in the b.py, if i convert from a.py import * to explicit form, from a.py import something, there occury errors that depend on exp1 library. There are a lot of example like that.

How do I solve the problem?

UPDATE

What did I? I import libraries with their own methods, variables to each library. For example I have a.py, b.py and c.py libraries. I import a.py and b.py with their own all methods to c.py. Like that,

from a import foo, bar from b import exp, solv

In b.py, in the same way, from a import foo, bar from c import sa, as

After that for now, libraries have necessary methods from real owner, not a 3rd library.

In the end, I delete unused methods in libraries.

1

There are 1 answers

1
vestronge On

Not sure if there is a easy solution, but this might help a little. Install pylint and set tests on all modules for "variable not defined"

  1. Create a module, say, "temporary.py" and have all your "from xyz import *" from all files in this place.
from name3 import *
from name2 import *
from general_functions import *
from messages import *
from network import *
from keyboard_functions import *
from process_functions import *
from name4 import *
from settings import *
  1. Remove all "from xyx import *" from all other modules and replace with single line "from temporary import *

This will help against depedencies of all modules on each other(as all modules will now depend on "temporary.py

  1. Now start with the first import "from name3 import *" in temporary.py. Remove this import and run the pylint tests. Fix all modules with failures by adding the implicit imports after "from temporary import *"

  2. Follow this step for all other imports

  3. Once all done, remove "from temporary import *" from all files.

Note: There is a caveat though. If 2 modules have same variable/function defined, say "my_function", and module C imports both modules, then it uses "my_function" from the last import. You wont be able to identify such issues if any and might end up using the wrong function.