如何从python导入路径中删除当前目录 [英] How to remove current directory from python import path
问题描述
我想使用hg
本身的Mercurial存储库.也就是说,我从 https://www.mercurial-scm.org/repo/hg ,并希望在克隆的存储库中运行一些hg
命令.问题是,在此克隆hg
可执行文件中运行hg
时,它尝试从此目录而不是从/usr/lib/pythonVERSION
等加载目录中的python模块.据我所知,这是因为Python导入路径sys.path
包含空字符串作为第一个条目,可能意味着当前目录". PYTHONPATH
环境变量未设置.
I want to work with the mercurial repository of hg
itself. That is, I cloned Mercurial from https://www.mercurial-scm.org/repo/hg and want to run some hg
commands inside the cloned repository. The problem is that when running hg
inside this clone hg
executable tries to load its python modules from this directory and not from /usr/lib/pythonVERSION
etc. As I understand it this happens because Python import path sys.path
contains an empty string as first entry which probably means "current directory". PYTHONPATH
environment variable is not set.
问题是如何防止已安装的hg
导入错误的"模块.
The questtion is how can I prevent my installed hg
from importing "wrong" modules.
推荐答案
我处理该主题的方法是创建一个具有以下内容的/usr/local/bin/hg
sh脚本:
The way I would deal with the topic is by creating a /usr/local/bin/hg
sh script with the following contents:
#!/bin/sh
PYTHONPATH=/usr/lib/pythonVERSION/site-packages /usr/bin/hg
(基于Ubuntu的发行版使用dist-packages
而不是site-packages
)
(Ubuntu-based distributives use dist-packages
instead of site-packages
)
PYTHONPATH
是Python解释器尊重的特殊环境变量,用于获取额外的模块导入路径.
PYTHONPATH
is a special environment variable respected by Python interpreter to get extra module import paths.
或者,您可以将PYTHONPATH
导出到您的shell中,但这会影响您的整个体验.
Alternatively, you can export PYTHONPATH
into your shell, but it will affect your whole experience.
这篇关于如何从python导入路径中删除当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!