hi there serdar yagi love here for infoworld in this episode of smart python we're going
to look at import lib a standard library module that allows you to import modules and libraries
programmatically most of the time in python when you import code it's by way of hard-coded
declarations at the top of a module sometimes though you want to perform imports dynamically
for instance only when a particular module is needed or when you want to choose between
a few possible modules import lib exposes mechanisms to do that within your python code
let's start with the most basic use of import lib in this simple example i'm using import lib
to import another module by name using a string the string is hard coded in this example it's it's
part of the actual source code but i could just as easily obtain it from user input or reading from a
file or an environment variable or what have you i also have in the same directory as this code
another module named other.pi it doesn't do very much it just prints a message when it's imported
and it has a single variable within it that we could look up by its name so when i run this main
code here's what happens import lib seeks out a module with the name that matches this string
imports it and makes it available in the variable other i can then use other as i would if i had
imported it using the usual import command the only difference is we don't use a hard-coded
import statement well except for fetching import lib that is now when we use import
module we're passing a string that is the same as what we would pass to the import statement itself
but if we wanted to import from a python file by way of a file path instead of the module
name that's used in python we would need to do this a little bit differently so in this
second example i'm showing you here how to import from a file path rather than just a module name
and it uses a slightly different mechanism it uses the spec from file location utility
and this takes in two parameters first is the module name we want to use for the file
in question which could include a dot path for instance if we're importing a sub module
second is the path to the file that we want to use for that module now when we do this it doesn't
actually give us the module itself at least not yet instead it gives us what's called a module
spec object and this is an object that describes to the import system how a given module is to be
imported its name its origin and a number of other bits of state that are required to go with it
and then we then use the specs loader object to actually load the module into a variable
and as you can see from when we run this code we now have other dot pi loaded and available
the process of loading from some arbitrary file is a little bit more convoluted but
there are reasons for that for one we have to specify what the module's fully qualified name
is going to be when we load it we can't just grab any old file and assign it to a variable
but fortunately the whole thing can be reduced to a simple recipe like the one we've got here
now exercise for the reader see if you can condense everything here into one line it's
actually not that hard but i will reveal an answer at the end of the video now let's take a look
at a more complex example i have here two modules other and other two and i want to load one
or the other based on some input at the command line other will be selected by default so in the
code for this example first thing i'm going to do is create a set of all the names of the modules in
the directory that match a certain wildcard other another two so that i can compare the
user's input against them as a way of verifying that i'm loading something that exists this is
strictly for my own sake next i've got the import mechanism which i've got wrapped up in a function
and then in the main body of the program i process the first user supplied argument
at the command line assuming there is one and i load the appropriate library based on that input
so if we run this with no arguments at all we will get other as our default module because it's
specified by default and if we supply a nonsense argument we'll get the same thing because other is
is again supplied by default but if we supply other two that will load that second module so one
advantage of loading loading modules selectively is that if the process of loading a module is
expensive if you have a lot of objects in it or it does a lot of things that might take some time
you can speed things up by only loading the module you need when you need it and there are also some
other mechanisms in python worth exploring to this end and i'm going to go into those in future
videos some of them are in the import lib module itself but they deserve full expanded treatment
now i should also point out that one of the big downsides to a dynamic module import is that it
makes type hinting a little bit more complicated because the import mechanisms are dynamic anything
from a dynamically imported module isn't going to have its type information preserved because
the the system has no way of knowing what's being imported and so that information cannot be passed
along example here you'll notice other.x in this example if i hover is hinted as any
but the variable that we derive from other dot x we can hint directly as integers so that's
one way to work around this but it's not perfect and it means that a lot of the the ease of use
that we would get from the type hinting would get erased if we start using dynamic imports
in this fashion so you have to be careful with it if you're relying on type hints quite a bit
okay remember earlier i said there was a way to condense all of this into one line and that
it isn't that hard well here is my solution and you can always wrap all of this up in a function
if you want to do that but this is the quick and dirty way to do it now i imagine anybody
who's reasonably conversant with python will have figured this out but this should give you an idea
of how you can do the whole thing in one step if the need ever does arise so that's it for
this video we will go into more about import live in the future but for now if you liked it please
leave a comment below don't forget to subscribe to the tech talk channel on youtube and for more
smart python and more device serdar be sure to follow me on facebook youtube and infowl.com