Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...