Polymorphic imports

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed Sep 22 12:41:47 EDT 2021


On Tue, 21 Sep 2021 18:58:31 +0000, Travis Griggs <travisgriggs at gmail.com>
declaimed the following:


>from lib import paths
>import paths.dynamic_client_module()
>
>But this seems to not work. Import can only take real modules? Not programatic ones?

	Consider "import" to be equivalent to a compile-time operation. 

https://docs.python.org/3/using/cmdline.html#environment-variables cf
PYTHONPATH

https://docs.python.org/3/library/sys.html#sys.path
"""
 sys.path

    A list of strings that specifies the search path for modules.
Initialized from the environment variable PYTHONPATH, plus an
installation-dependent default.

    As initialized upon program startup, the first item of this list,
path[0], is the directory containing the script that was used to invoke the
Python interpreter. If the script directory is not available (e.g. if the
interpreter is invoked interactively or if the script is read from standard
input), path[0] is the empty string, which directs Python to search modules
in the current directory first. Notice that the script directory is
inserted before the entries inserted as a result of PYTHONPATH.

    A program is free to modify this list for its own purposes. Only
strings and bytes should be added to sys.path; all other data types are
ignored during import.
"""

	So... Putting the module variants into separate directories, and
modifying the import path to reference the directory of a specific variant,
might do what you want -- as long as the module name itself remains fixed.

	The other alternative may be
https://docs.python.org/3/library/functions.html#__import__


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Python-list mailing list