1

I have three files in total in python 2.7:

  1. A module file in some directory (e.g. module1.py)
  2. A different module in the same directory, which imports this module (e.g. worker.py)
  3. A main script in the top-level directory importing worker.py

When the file worker.py looks as the following

import module1 as mod

everything works as expected (i.e. I can use worker.mod.XXX in my main code). But when I replace the content of worker.py as follows (which I expected to do the same):

mod = __import__('module1')

I get an error: ImportError: No module named module1. I need the latter way to handle things to automate importing modules from a list.

What am I missing here?

To be more precise: I am just looking for a way to replace the statement import module1 as mod by an expression in which module1 is a string. I have e.g. modulname='module1', and want to import the module with the name of the module given in the string modulname. How to do that?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • I believe there should be another factor, because `__import__()` is directly invoked by the `import` statement. – Zaur Nasibov Dec 07 '12 at 08:14
  • What factor do you mean? – Alex Dec 07 '12 at 08:27
  • Well, for example does this error appears ONLY when you change this particular line of code (and absolutely everything else, including how the program is launched is the same)? – Zaur Nasibov Dec 07 '12 at 08:39
  • Yes. I only try to import the same module in a different way, everything else is the same. – Alex Dec 07 '12 at 08:46

2 Answers2

3

__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module

Import a module. Because this function is meant for use by the Python interpreter and not for general use it is better to use importlib.import_module() to programmatically import a module.

The globals argument is only used to determine the context; they are not modified. The locals argument is unused. The fromlist should be a list of names to emulate from name import ..., or an empty list to emulate import name. When importing a module from a package, note that __import__('A.B', ...) returns package A when fromlist is empty, but its submodule B when fromlist is not empty. Level is used to determine whether to perform absolute or relative imports. -1 is the original strategy of attempting both absolute and relative imports, 0 is absolute, a positive number is the number of parent directories to search relative to the current module.

aychedee
  • 24,871
  • 8
  • 79
  • 83
Denis
  • 7,127
  • 8
  • 37
  • 58
  • I tried to use `importlib`, but `importlib.import_module('module1')` gives the same error. – Alex Dec 07 '12 at 08:17
  • OK, how to place module which you want to import against place where you do import? – Denis Dec 07 '12 at 08:27
  • First, I do not understand your comment. Secondly, I am just looking for a way to replace `import module1 as mod` by an expression in which `module1` is used as string. I have e.g. `modulname='module1'`, and now I want to import the module with the name as defined in the string `modulname`. How to do that? – Alex Dec 07 '12 at 08:30
  • At first try to understand how MRO working in Python. This is a really ordinary theme which discussed here many times. Read this http://stackoverflow.com/questions/10287054/how-to-import-the-src-from-the-tests-module-in-python – Denis Dec 07 '12 at 08:34
  • This is not helpful. This does not answer my question – Alex Dec 07 '12 at 08:36
  • @Alex: I don't think you'll get much more help here if you continue making comments like that :) – Ber Dec 07 '12 at 08:39
  • 1
    Man, this is helpful, if you do not understand the basics nobody can't help you. – Denis Dec 07 '12 at 08:39
  • I really do not understand your comments. Denis explanation is not helpful. What does it mean that `globals` is not modified? What does it mean that `locals` is unused? I do not understand. And the link provided by Denis does not seem to be related to my problem at all. – Alex Dec 07 '12 at 08:44
  • This document neither mentions `__module__` not `globals` nor `locals`. It seem to be an interesting historical document in the development of python imports, but unrelated to my problem. Also no person so far has mentioned the basic problem with my approach. – Alex Dec 07 '12 at 08:52
2

try this:

mod = __import__('module1', globals=globals())
0xc0de
  • 8,028
  • 5
  • 49
  • 75