An answer from the perspective of someone who programs in both Python and TeX.
In Python, by default if a package does something like
def f():
pass
def g():
f()
the functions f and g are defined in the "global" namespace, but it's not "true global", rather it's just the "module global",
as such it does not interfere with the code that imports it.
If you do from my_module import g, then when g is called and it tries to call f, the name f is looked up in the globals
of the module my_module, which does not require the f itself to be available in the globals of the importing code.
In TeX however, by default everything are in true global scope; furthermore, by default all the names are looked up in the global scope.
For an analog, Python has a "true global" namely builtins module, variables of this module is available to every modules.
In that case the TeX equivalent of the snippet above would be
import builtins
def f():
pass
builtins.f=f
def g():
builtins.f()
builtins.g=g
In this case, even if you copy the g to some other name,
for it to work correctly the global function named f must be the f of this module.
Which explains the difficulty when you try to import two different modules which both has functions named f and g.
From the TeX perspective, the equivalent of the above would be
\cs_new_protected:Npn \__mymodule_f: {
}
\cs_new_protected:Npn __mymodule_g: {
__mymodule_f:
}
then an \usepackage{mymodule} would make the names available as \__mymodule_f: and \__mymodule_g: respectively,
and the equivalent of from mymodule import f would be the above plus \cs_new_eq:NN \f \__mymodule_f:.
Needless to say, most user-level modules are not implemented this way because the typical user have no idea what \cs_new_eq:NN is.
(although to be fair it's still possible to make a command namely \importFromMymodule \f to explicitly define the \f etc.
and \importAllFromMymodule etc. but usually nobody needs these things anyway)
Nevertheless, if you really insist, most of the time the "command juggling" can be done by
\usepackage{first package that defines f}
\NewCommandCopy \firstPackageF \f % optional, if you want to call the first package's \f
\let \f \undefined % delete the first package's \f so the second package can define it -- as explained above any "\g" in the first package might cease to work
\usepackage{second package that defines f}
For "nicely implemented" packages this should mostly work.
In the rare few cases that it doesn't, restoring all the names that the hidden package is needed before calling the functions of the
old package.
{\usepackage{package} \global\let\macro=\macro}. Alternatively, if what you really want to do is save the definition of a macro before you load a package you can try\let\oldmacro=\macro \usepackage{package} \let\macro=\oldmacro. – Slurp Dec 28 '22 at 08:53catchfilebetweentagspackage to load only certain parts of the code. – samcarter_is_at_topanswers.xyz Dec 28 '22 at 13:53