English 中文(简体)
Python 看不到我的子包包
原标题:Python does not see my sub-package

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内, 但我不知道这是否重要。

最佳回答

这是由 cime (不管是什么) 开发者做出的设计决定。 软件包不会神奇地导入全部子软件包。 这是件好事, 因为否则像 SciPy 这样的大型软件包将永远导入( 像 NLTK 那样 ) 。

某些软件包, 如 os.path 、 NumPy 和 NLTK 将导入子模块, 但是它们会明确输入子模块。 如果您想要在您自己的模块中发生这种情况, 请做

# __init__.py
import .submodule
问题回答

os.path 是一个与执行细节有关的例外。 Python 通常对它要做多少工作比较保守, 所以它只导入您实际想要的模块。 因为 pos. path 的实际执行相当取决于您的操作系统, Python 能够确定您需要执行什么, 在 python 启动时导入为 . path

您可以在您的 cime/ __ init_. py 中添加一些额外的导入, 从而产生非常相似的效果, 如果这是你想要的的话。

from cime import runners

诸如此类。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...