4

I would like to import a module from inside a functions. For example from this:

from directory.folder.module import module

def import():   
    app.register_blueprint(module)

To this:

def import():   
    from directory.folder.module import module

But, without hardcoding it. For example:

def import():
    m = "module"
    from directory.folder.m import m

Is it possible? Thanks in advance

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
BigMeister
  • 361
  • 2
  • 11

3 Answers3

9

You want the importlib module.

Here's the most simplistic way to use this module. There are lots of different ways of weaving the results of calls to the module into the environment:

import importlib

math = importlib.import_module("math")

print(math.cos(math.pi))

Result:

-1.0

I've used this library a lot. I built a whole plug-in deployment system with it. Scripts for all the various deploys were dropped in directories and only imported when they were mentioned in a config file rather than everything having to be imported right away.

Something I find very cool about this module is what's stated at the very top of its documentation:

The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the import() function) in Python source code.

The intro in the 2.7 docs is interesting as well:

New in version 2.7.

This module is a minor subset of what is available in the more full-featured package of the same name from Python 3.1 that provides a complete implementation of import. What is here has been provided to help ease in transitioning from 2.7 to 3.1.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Well, that's what I always think. But isn't this an "illegal" answer? – CryptoFool Nov 19 '20 at 16:58
  • So, I suppose your answer is the most secure but for some reason it seems not working for my specific case. Maybe I am doing something wrong, It could be. I solved with the answer from Wasif. Thanks a lot man, I appreciated your effort and I would like that you answer will still be on the top too! – BigMeister Nov 19 '20 at 17:27
  • @BigMeister How about `m = getattr(importlib.import_module('directory.folder.m'), 'm')`? – superb rain Nov 19 '20 at 17:40
  • @superbrain SUPERB! This is exactly what I wanted to see. Works like a charm! :D – BigMeister Nov 19 '20 at 18:01
  • @BigMeister That's btw from [here](https://stackoverflow.com/a/8790232/13008439), which is an old answer for Python 2 but apparently still proper. – superb rain Nov 19 '20 at 18:11
2

No, python import does not work this way.

Such as an example you try to import a module named mod, so you run import mod. Now interpreter will search for mod.py in a list of directories gathered from the following sources:

  • The directory from where the input script was run or the current directory if the interpreter is being run interactively.

  • The list of directories contained in the PYTHONPATH environment variable, if it is set. (The format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)

  • An installation-dependent list of directories configured at the time Python is installed.

So if you have a variable named m='mod' and run import m it will search for m.py not mod.py.

But just a silly dangerous workaround is to use exec() (WARNING FOR MALICIOUS INPUT)

m = "module"
exec(f'from directory.folder.m import {m}')

If you don't mind external modules try importlib.

Wasif
  • 14,755
  • 3
  • 14
  • 34
2

You can use the importlib module to programmatically import modules.

import importlib

full_name = "package." + "module"

m = importlib.import_module(full_name)
sphennings
  • 998
  • 10
  • 22