sys.path()和PYTHONPATH问题 [英] sys.path() and PYTHONPATH issues

查看:139
本文介绍了sys.path()和PYTHONPATH问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Python,我在2.7.3中工作,我正在努力了解 import 语句。

I've been learning Python, I'm working in 2.7.3, and I'm trying to understand import statements.


  1. 文档说,当您尝试导入模块时,解释器将首先搜索其中一个内置模块。

  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?

然后,文档说,解释器搜索sys.path列出的目录,并且sys.path被初始化从这些来源:

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


  • 包含输入脚本(或当前目录)的目录。

  • PYTHONPATH (目录名列表,与shell变量 PATH 的语法相同)

  • 与安装相关的默认值。

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

这是从我的电脑使用的sys.path命令的示例输出python命令线路模式:(我删除了一些这样不会很大)

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']

现在,我假设''路径指的是包含'script'的目录,所以我认为其余的将来自我的 PYTHONPATH 环境变量。但是,当我去终端并键入 env PYTHONPATH 不存在作为一个环境变量。我也尝试过 import os 然后 os.environ ,但我得到相同的输出。

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.

我真的没有PYTHONPATH环境变量吗?我不相信我曾经专门定义了一个PYTHONPATH环境变量,但是我假设当我安装了新的软件包时,它们会自动更改环境变量。如果我没有PYTHONPATH,我的sys.path如何填充?如果我下载新的软件包,Python如何知道在哪里找不到这个PYTHONPATH变量?

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?

环境变量如何工作?根据我的理解,环境变量特定于它们设置的进程,但是,如果我打开多个终端窗口并运行 env ,它们都显示一些相同的变量,例如 PATH 。我知道持久环境变量的文件位置,例如 / etc / environment ,其中包含我的 PATH 变量。是否可以告诉存储持久环境变量的位置?用于存储新的持久性环境变量的推荐位置是什么? Python解释器如何实际使用环境变量? Python解释器寻找 PYTHONPATH ,但是它的工作原理如何?

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?


推荐答案

一次有这么多问题! :)

So many questions in one go! :)

我只想回答其中的一些。

Well I try to answer only a few of them.

1)内置模块是python发行版的任何模块。例如,sys和os模块是内置的模块。这是真的。

1) a built-in module is any module that comes with a python release. For instance the sys and os modules are built-in modules. That's it really.

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

2) The PYTHONPATH variable don't exist by default on your system. When you launch the python interpreter, it fills the array of path where it search for modules, in the way you described. This is the result of sys.path. However sys.path is not the environment variable PYTHONPATH. If you set the PYTHONPATH in your system, then all the path contained in it will be included in the array that python's interpreter uses to search for modules.

我会把答案留给别人的环境变量,因为我觉得我是回答这个问题的正确的人。
我的感觉是,它可能会从系统改变到系统。无论如何...

I will leave the answer to the environment variables for others, as I don't feel I'm the right person to answer such a question. My feeling though, is that it might change from system to system. Anyway...

希望有帮助。

这篇关于sys.path()和PYTHONPATH问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆