<p>您缺少修改<code>sys.path</code>或<code>PYTHONPATH</code>变量以同时包含<strong>foo/</strong>所在的目录。你知道吗</p>
<p>在运行<strong>客户端</strong>文件执行以下操作</p>
<pre><code>codepython@vm-0:~/python/foo$ PYTHONPATH=$PYTHONPATH:~/python/;export PYTHONPATH
</code></pre>
<p>在我的设置中,<strong>foo/</strong>出现在<strong>~/python</strong>中,<strong>foo/</strong>中的<strong>\em>init</em>\upy</strong>告诉python解释器,<strong>foo</strong>是一个包,而不是一个普通目录。你知道吗</p>
<p>现在您可以<code>cd</code>进入<strong>foo/</strong>目录,然后运行<code>python cli.py</code>。您将能够访问<strong>foo</strong>包方法和函数。你知道吗</p>
<p>或者,在main()中,您可以首先修改<code>sys.path</code>,以附加包含<strong>foo/</strong>的目录,然后执行其余的逻辑</p>
<p>引用<a href="http://docs.python.org/2/tutorial/modules.html#packages" rel="nofollow">Python documentation on Modules</a>:</p>
<blockquote>
<p>When a module named spam is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named spam.py in a list of directories given by the
variable sys.path. sys.path is initialized from these locations:</p>
<p>the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the
shell variable PATH). the installation-dependent default. After
initialization, Python programs can modify sys.path. The directory
containing the script being run is placed at the beginning of the
search path, ahead of the standard library path. This means that
scripts in that directory will be loaded instead of modules of the
same name in the library directory. This is an error unless the
replacement is intended. See section Standard Modules for more
information.</p>
</blockquote>