sys.path() and PYTHONPATH issues

后端 未结 3 1407
长情又很酷
长情又很酷 2021-01-05 22:05

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

  1. The documentation says that when you attemp

相关标签:
3条回答
  • 2021-01-05 22:40
    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.

    0 讨论(0)
  • 2021-01-05 22:40

    PYTHONPATH is not defined on your system. That means all you see in sys.path except for '' is "the installation-dependent default."

    Environment variables characterize the environment, not a process. However, you can tweak them when starting a process by using, say, Linux env command. That basically means that the process will run in a different environment. The "how if works" part of your question can have platform-dependent answers. However, I don't think you can "tell where a persistent environment variable is stored", if you mean a file. Environment variables can be set in any of the files that are executed at some point (usually at startup) or just in the command line.

    0 讨论(0)
  • 2021-01-05 22:45

    So many questions in one go! :)

    Well I try to answer only a few of them.

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

    Hope it helps.

    0 讨论(0)
提交回复
热议问题