Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide external modules when importing a module (e.g. regarding code-completion)

I have several modules in one package (a kind of a toolkit), which I use in my projects. The structure looks like this:

the_toolkit:     __init__.py     basic_io.py     simple_math.py     matrix_kit.py     ... 

Now when I use IPython or the code completion in VIM after importing a module from the package with

from the_toolkit import matrix_kit 

I get a bunch of modules/functions which matrix_kit.py imports (array, math, log, loggin, format_exc, etc.), along with the actual functions which matrix_kit defines. How can I filter out everything else, since I'm not interested in math or array in a code completion of the matrix_kit module?

The __init__.py is empty as suggested by many pythonistas, or is it the way to do it?

like image 446
tamasgal Avatar asked May 12 '13 15:05

tamasgal


People also ask

Which is the correct way to import the module in your program?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

How do I import a module from the outside directory?

Method 1: Using sys. The sys. path variable of the module sys contains the list of all directories in which python will search for a module to import. We can directly call this method to see the directories it contains. So for importing mod.py in main.py we will append the path of mod.py in sys.

What is external module in Python?

External modules in Python are important since they allow developers to break down large programs into small manageable files. Furthermore, modules also allow developers to reuse code. Once a Python file has been imported as a module into another file it gives us access to its classes and functions.

What is a module in Python and how is module imported to the program?

In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.


1 Answers

I had the same problem, and solved it by adding a leading underscore to all my imports. It doesn't look brilliant, but it achieves what you're after.

from __future__ import division as _division import numpy as _np import pandas as _pd 

Only the stuff that starts without an underscore is imported when you import the module using from .. import *. or when you use tab-completion in IPython or spyder.

like image 146
jkokorian Avatar answered Oct 11 '22 20:10

jkokorian