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?

asked Dec 7, 2012 at 8:09
4
  • I believe there should be another factor, because __import__() is directly invoked by the import statement. Commented Dec 7, 2012 at 8:14
  • What factor do you mean? Commented Dec 7, 2012 at 8: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)? Commented Dec 7, 2012 at 8:39
  • Yes. I only try to import the same module in a different way, everything else is the same. Commented Dec 7, 2012 at 8:46

2 Answers 2

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
25.7k8 gold badges82 silver badges83 bronze badges
answered Dec 7, 2012 at 8:13
Sign up to request clarification or add additional context in comments.

9 Comments

I tried to use importlib, but importlib.import_module('module1') gives the same error.
OK, how to place module which you want to import against place where you do import?
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?
At first try to understand how MRO working in Python. This is a really ordinary theme which discussed here many times. Read this stackoverflow.com/questions/10287054/…
Man, this is helpful, if you do not understand the basics nobody can't help you.
|
2

try this:

mod = __import__('module1', globals=globals())
answered Dec 7, 2012 at 9:03

1 Comment

Thank you so much!! This solves my problems with a concrete example.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.