sys.path()和PYTHONPATH问题

7

我一直在学习Python,使用的版本是2.7.3,现在我正在尝试理解import语句。

  1. The documentation says that when you attempt to import a module, the interpreter will first search for one of the built-in modules.

    What is meant by a built-in module?

  2. Then, the documentation says that the interpreter searches in the directories listed by sys.path, and that sys.path is initialized from these sources:

    • 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.

    Here is a sample output of a sys.path command from my computer using python in command-line mode: (I deleted a few so that it wouldn't be huge)

    ['', '/usr/lib/python2.7', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
    

    Now, I'm assuming that the '' path refers to the directory containing the 'script', and so I figured the rest of them would be coming from my PYTHONPATH environmental variable. However, when I go to the terminal and type env, PYTHONPATH doesn't exist as an environmental variable. I also tried import os then os.environ, but I get the same output.

    Do I really not have a PYTHONPATH environmental variable? I don't believe I ever specifically defined a PYTHONPATH environmental variable, but I assumed that when I installed new packages they automatically altered that environment variable. If I don't have a PYTHONPATH, how is my sys.path getting populated? If I download new packages, how does Python know where to look for them if I don't have this PYTHONPATH variable?

  3. How do environment variables work? From what I understand, environment variables are specific to the process for which they are set, however, if I open multiple terminal windows and run env, they all display a number of identical variables, for example, PATH. I know there file locations for persistent environment variables, for example /etc/environment, which contains my PATH variable. Is it possible to tell where a persistent environment variable is stored? What is the recommended location for storing new persistent environment variables? How do environment variables actually work with say, the Python interpreter? The Python interpreter looks for PYTHONPATH, but how does it work at the nitty-gritty level?


有关 sys.path 如何工作的更多信息,请参见此回答 https://dev59.com/InNA5IYBdhLWcg3wmfIa#38403654 - djhaskin987
3个回答

7

一次提出了这么多问题! :)

我会尽量回答其中的几个。

1)内置模块是指随着Python发布而附带的任何模块。例如,sys和os模块都是内置模块。就是这样。

2)PYTHONPATH变量在您的系统上默认不存在。当您启动Python解释器时,它会以您描述的方式填充路径数组,在其中搜索模块。 这就是sys.path的结果。但是sys.path不是环境变量PYTHONPATH。 如果您在系统中设置了PYTHONPATH,则其中包含的所有路径都将包含在Python解释器用于搜索模块的数组中。

至于环境变量的答案,我会留给其他人回答,因为我感觉我不是回答这种问题的合适人选。 不过,我的感觉是它可能因系统而异。无论如何...

希望对您有所帮助。


2
  1. Built-in modules are the modules listed under sys.builtin_module_names. These modules are compiled together with the interpreter, and hence are always available. Note that they are part of the standard library but the standard library includes many more modules. To make the difference clear, if you try to run this code in python3.3:

    >>> import sys
    >>> sys.path = []
    >>> import os          # works
    >>> import traceback   # ImportError, traceback is in the stdlib!
    

    On python2 both succeed because the import special-cases the standard library path. (See this for more information on the changes to import in python3.3).

  2. If the PYTHONPATH isn't found then it doesn't include any extra directory. However the documentation you link also states that python uses an "installation-dependent default". The directories you are seeing in your sys.path are defined in this "installation-depedent default". If you define the PYTHONPATH you can add other directories to sys.path before the default one, but no harm is done without defining it.

  3. Environment variables are defined per-process however shells can provide their own "scope" of variables to the subprocesses they launch. For example you can set environment variables in the shell files like ~/.bashrc or ~/.profile.

    If you are on Ubuntu the preferable way of defining a system wide and persisten environment variable is using the pam_enviroment. You can see this question on Ask Ubuntu that shows how you can set a system wide environment variable.

    AFAIK there is no standard way of setting them for every (e.g.) linux distro, and, obviously, each OS has its own method of defining them.

    If you want a more detailed explanation about environment variables handling you should probably ask on Super User.


非常好的答案! - lmiguelvargasf
@Baikuriu,总之,我可以使用PYTHONPATH作为一种添加其他目录的方式,以便我的Python解释器查找模块和包,对吧? - lmiguelvargasf
@lmiguelvargasf 是的。 - Bakuriu

0

PYTHONPATH 在您的系统上未定义。这意味着您在 sys.path 中看到的除了 '' 之外的所有内容都是“安装相关的默认值”。

环境变量描述的是环境,而不是进程。但是,您可以通过使用 Linux 的 env 命令在启动进程时调整它们。这基本上意味着该进程将在不同的环境中运行。您的问题中的“它如何工作”的部分可能会有平台相关的答案。但是,如果您指的是文件,我认为您无法“告诉持久环境变量存储在哪里”。环境变量可以设置在任何在某个时刻(通常在启动时)执行的文件中,或者只是在命令行中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接