奇怪的python sys.path值

时间:2013-07-25 05:40:03

标签: python python-import pythonpath sys.path

除了空字符串...

之外,请注意sys.path的最左边的值

从根目录python -c "import sys; print(sys.path)"给了我:

['', '/home/brian/code/indep/chimai', '/home/brian/code/indep/google_search/src', '/usr/lib/python33.zip', 
'/usr/lib/python3.3', '/usr/lib/python3.3/plat-linux', '/usr/lib/python3.3/lib-dynload', '/usr/lib/python3.3/site-packages']

来自我的主目录

['', '/home/brian/home/brian/code/indep/chimai', '/home/brian/code/indep/google_search/src', 
'/usr/lib/python33.zip', '/usr/lib/python3.3', '/usr/lib/python3.3/plat-linux', '/usr/lib/python3.3/lib-dynload', 
'/usr/lib/python3.3/site-packages']
来自/ boot / grub的

['', '/boot/grub/home/brian/code/indep/chimai', '/home/brian/code/indep/google_search/src', '/usr/lib/python33.zip', 
/usr/lib/python3.3', '/usr/lib/python3.3/plat-linux', '/usr/lib/python3.3/lib-dynload', '/usr/lib/python3.3/site-packages']

无论我测试哪个目录,此行为都会继续。也就是说,您看到的sys.path的第二个和第三个值应该从我的PYTHONPATH变量加载,但是第一个值总是将我当前的目录附加到它的前面。

此外,由于某些原因,python -Sc "import sys; print(sys.path)不会这样做。有了这个命令,我总是得到:

['', 'home/brian/code/indep/chimai', '/home/brian/code/indep/google_search/src', '/usr/lib/python33.zip', 
'/usr/lib/python3.3/', '/usr/lib/python3.3/plat-linux', '/usr/lib/python3.3/lib-dynload']

这一切都非常令人惊讶。 site.py对此负责吗?有人能指出我正确的方向吗?我只是想导入我自己的模块,目前由于某种原因无法使用。

1 个答案:

答案 0 :(得分:0)

来自site.py的文档字符串:

"""Append module search paths for third-party packages to sys.path.

****************************************************************
* This module is automatically imported during initialization. *
****************************************************************

In earlier versions of Python (up to 1.5a3), scripts or modules that
needed to use site-specific modules would place ``import site''
somewhere near the top of their code.  Because of the automatic
import, this is no longer necessary (but code that does it still
works).

This will append site-specific paths to the module search path.  On
Unix (including Mac OSX), it starts with sys.prefix and
sys.exec_prefix (if different) and appends
lib/python<version>/site-packages as well as lib/site-python.
On other platforms (such as Windows), it tries each of the
prefixes directly, as well as with lib/site-packages appended.  The
resulting directories, if they exist, are appended to sys.path, and
also inspected for path configuration files.

A path configuration file is a file whose name has the form
<package>.pth; its contents are additional directories (one per line)
to be added to sys.path.  Non-existing directories (or
non-directories) are never added to sys.path; no directory is added to
sys.path more than once.  Blank lines and lines beginning with
'#' are skipped. Lines starting with 'import' are executed.

For example, suppose sys.prefix and sys.exec_prefix are set to
/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
with three subdirectories, foo, bar and spam, and two path
configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
following:

  # foo package configuration
  foo
  bar
  bletch

and bar.pth contains:

  # bar package configuration
  bar

Then the following directories are added to sys.path, in this order:

  /usr/local/lib/python2.5/site-packages/bar
  /usr/local/lib/python2.5/site-packages/foo

Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is
omitted because it is not mentioned in either path configuration file.

After these path manipulations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
site-specific customizations.  If this import fails with an
ImportError exception, it is silently ignored.

"""

就导入您自己的模块而言,以下规则适用:

如果模块在当前工作目录中,如果它位于当前工作目录的子目录中,则可以使用简单import mymodule导入它,如果有包,则可以将其导入为subdir.mymodule当前工作目录的子目录中的模块包含一个名为__init__.py的文件,该文件声明了一个名为__all__ = ['modulename', 'another', 'etc']的列表,然后您可以from subdir import modulename import subdir并使用subdir.etc.fn()

如果您的模块是需要在多个地方使用但不能在整个地方使用的模块,则可以将其放在一个公共位置,然后在需要时将该位置附加/插入到sys.path中。如果它只是您需要的东西,那么您可以将它添加到您的站点包,方法是将其放在适当的位置并添加.pth文件。最后,如果您认为其他人将从您的包装中受益,您可以使用distutils捆绑它并将其添加到奶酪店或其他地方分发。