0

If I import a python module using a syntax like this:

import my_module

I can later use a number of simplest command to get an idea of what the module is, where it is located and etc. For example:

print(my_module) 

outputs: module 'my_module' from 'my_module.py'

dir(my_module)

outputs: ['MyClass', 'builtins', 'doc', 'file', 'name', 'package', 'math', 'os', 'sys']

I even can find out an absolute path of the module by using:

print os.path.abspath( my_module.__file__  )

outputs: /Users/julia/Documents/classes/my_module.py

But if instead of import 'my_module' I would be using:

from my_module import MyClass

all I can is to:

print MyClass

which outputs: my_module.MyClass

I do see MyClass came from my_module file. But unfortunately that is all I can get since none of the commands I used to use to get the info on module doesn't work. Here is use and their output:

print dir(my_module.MyClass)  NameError: name 'my_module' is not defined

print dir(my_module)  NameError: name 'my_module' is not defined

print my_module  name 'my_module' is not defined

What command(-s) should I be using while tracking down the imported modules brought with

from my_module import MyClass

syntax?

2
  • 1
    huh? whats the question? if you just import MyClass ... you have only imported that class... Commented Jan 8, 2014 at 17:39
  • this worked for me. perhaps you don't import that module at all, and want to print its detail. Commented Jan 8, 2014 at 17:40

2 Answers 2

1

The Problem is that

from my_module import MyClass

only imports MyClass but not the whole Module.

If you want to know the name of the Module MyClass was imported from, you can use:

print MyClass.__module__

If you want to use other Stuff from the same Module you could use:

my_module = __import__(MyClass.__module__)
print dir(my_module)

But it would be far more easy to just write:

import my_module
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Mr.Crash! Here is the summary:

If the module was imported with:

from my_module import MyClass 

don't call the module by using its name directly. Instead get to it via class imported from this module (using class's module attr) such as:

print dir(MyClass.__module__)
print os.path.abspath( MyClass.__module__ )

Comments

Your Answer

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.