从 Python 搜索模块路径中删除路径 [英] Removing path from Python search module path

查看:147
本文介绍了从 Python 搜索模块路径中删除路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 sys.path 指的是

  1. 具有系统库的操作系统路径.我认为这些是指 *nix 中的 /lib 或 Windows 上的 Windows.
  2. python 启动的当前目录 - 如果 Python 是从 C:\Python 启动的,我将采用它,这将是当前路径
  3. 环境变量 $PYTHONPATH 或 %PYTHONPATH% - 这是指我可以从命令行调用 Python 二进制文件的路径
  4. 您可以在运行时添加路径 - 我理解的是当我运行 IDLE 时
  1. OS paths that have your system libraries. I take it that these refer to /lib in *nix or Windows on Windows.
  2. current directory python started from - I take it if Python is started from C:\Python, this would be the current path
  3. environmental variable $PYTHONPATH or %PYTHONPATH% - This refers to the path where I can call the Python binary from the command line
  4. you can add paths at runtime - Which I understand to be when I run IDLE

我可以通过运行命令 sys.path.append 添加路径但是当我运行命令 sys.path.remove 来删除"我的路径时附加,我无法这样做.有没有办法不用每次都关闭IDLE?

I am able to add paths by running the command sys.path.append however when I run the command sys.path.remove to 'delete' the path I appended, I am unable to do so. Is there a way to do so without having to close IDLE each time?

我在 Windows 7 和 Ubuntu 上运行 Python 2.7

I am running Python 2.7 on Windows 7 as well as Ubuntu

推荐答案

在我的机器上一切正常 :)

Everything works as intended on my machine :)

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>> 

你到底尝试了什么?

关于你对事物的理解 - 恐怕有些误解:

Regarding your understanding of things - I'm afraid there are some mis-understandings:

  1. sys.path 是包含 Python 模块 的目录列表,而不是系统库.所以,简化一下,当你的脚本中有类似 import blah 的东西时,Python 解释器会一一检查这些目录以检查是否有一个名为 blah.py(或一个名为 blah 的子目录,里面有 __init__.py 文件)

  1. sys.path is a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something like import blah in your script, Python interpreter checks those directories one by one to check if there is a file called blah.py (or a subdirectory named blah with __init__.py file inside)

当前目录是脚本所在的位置,而不是 Python 解释器所在的位置.所以如果你在一个目录中有 foo.pybar.py,你可以在 foo.py 中使用 import barcode> 和模块会被找到,因为它在同一个目录中.

Current directory is where the script is located, not where Python interpreter is. So if you have foo.py and bar.py in a directory, you can use import bar in foo.py and the module will be found because it's in the same directory.

$PYTHONPATH 是在解释器启动时附加到 sys.path 的环境变量.所以,同样,它与模块搜索路径有关,与从命令行启动 Python 无关.

$PYTHONPATH is an environment variable which is getting appended to sys.path on interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.

正确,您可以在运行时修改 sys.path - 无论是在 IDLE 中运行 python 脚本时

Correct, you can modify sys.path at runtime - either when running a python script on in IDLE

参见 sys.pathsite 了解更多详情.

See sys.path and site for more details.

这篇关于从 Python 搜索模块路径中删除路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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