2

I am new to python. I have a very basic question. When we use the following command (I understand its not efficient to import everything using *) from _ (any module name) import *

How can I check the things that get imported using the above command?

Pratik
  • 61
  • 2
  • 7
  • Under phython documentation you can see what "this" module has and how you can use it. – Smit Feb 26 '17 at 01:26
  • It is a package i installed from github. I can always go and look all the files in github package but I wanted a method to know what I imported using "*" command – Pratik Feb 26 '17 at 01:29
  • @Pratik: This is exactly why you're discouraged from using `*` imports. You never know what `*` can drag into your global scope. – Blender Feb 26 '17 at 01:39
  • Also see http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python – PM 2Ring Feb 26 '17 at 03:02

1 Answers1

3

You can use dir to see what names are in the current module. By comparing the names before and after the import you can see what's imported:

>>> vars_before_import = set(dir())
>>> from json import *
>>> set(dir()) - vars_before_import
set(['load', 'JSONEncoder', 'dump', 'vars_before_import', 'JSONDecoder', 'dumps', 'loads'])

To exclude vars_before_import:

>>> set(dir()) - vars_before_import - {'vars_before_import'}
set(['load', 'JSONEncoder', 'dump', 'JSONDecoder', 'dumps', 'loads'])

NOTE

This won't catch objects that have been replaced (e.g. you defined load before importing everything in json).

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 2
    Although a minor detail, this won't catch objects that have been replaced (e.g. you defined `load` before importing everything in `json`). – Blender Feb 26 '17 at 01:30
  • @Blender, Thank you for the comment. I missed that. I will update the answer to include your comment. – falsetru Feb 26 '17 at 01:32