Why do I have to import all sub-packages if I want to use them?
I ll explain myself with an example:
In [1]: import cime
In [2]: cime.runners
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/miki/testit/<ipython-input-2-35e09c66121a> in <module>()
----> 1 cime.runners
AttributeError: module object has no attribute runners
In [3]: import cime.runners
In [4]: cime.runners
Out[4]: <module cime.runners from /home/miki/testit/venv/lib/python2.7/site-packages/cime/runners/__init__.pyc >
您可以看到它是一个普通的 Python 软件包 :
In [5]: cime.__file__
Out[5]: /home/miki/testit/venv/lib/python2.7/site-packages/cime/__init__.pyc
In [6]: ls /home/miki/testit/venv/lib/python2.7/site-packages/cime
download.py __init__.py log.py runners/ run.pyc utils.pyc
download.pyc __init__.pyc log.pyc run.py utils.py
但这不会发生在内置模块上 为什么?
In [7]: import os # instead of import os.path
In [8]: os.path
Out[8]: <module posixpath from /home/miki/testit/venv/lib/python2.7/posixpath.pyc >
Thanks,
rubik
P. S. 我在一个虚拟env内, 但我不知道这是否重要。