Why use sys.path.append(path) instead of sys.path.insert(1, path)?
编辑:基于Ulf Rompe的注释,重要的是你使用"1"代替"0",否则你将破坏sys.path。
我已经做了很长一段时间的python(超过一年),我总是很困惑为什么人们建议你使用
假设我正在开发一个名为PyWorkbooks的模块(安装在我的计算机上),但我同时正在开发一个包含PyWorkbooks的不同模块(比方说PyJob)。当我正在使用PyJob时,我发现PyWorkbooks中的错误正在纠正,所以我想导入一个开发版本。
有两种方法可以同时工作(例如,我可以将PyWorkbooks项目放在PyJob中),但有时我仍然需要使用路径。但是,我不能简单地在PyWorkbooks所在的文件夹中执行
这就是你必须做一个sys.path.insert(1,path_to_dev_pyworkbooks)的原因
综上所述:
1 2 | sys.path.append(path_to_dev_pyworkbooks) import PyWorkbooks # does NOT import dev pyworkbooks, imports installed one |
要么:
1 2 | sys.path.insert(1, path_to_dev_pyworkbooks) # based on comments you should use **1 not 0** import PyWorkbooks # imports correct file |
这对我来说已经引起了一些困扰,如果我们(作为一个社区)开始推荐
或者我有什么不对劲?这个问题有时会困扰我,我希望它在公开场合!
如果您有多个版本的软件包/模块,则需要使用virtualenv(强调我的):
virtualenv is a tool to create isolated Python environments.The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into
/usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.
Also, what if you can’t install packages into the global
site-packages directory? For instance, on a shared host.In all these cases,
virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).
这就是为什么人们认为
如果您确实需要使用sys.path.insert,请考虑保留sys.path [0]:
1 | sys.path.insert(1, path_to_dev_pyworkbooks) |
这可能很重要,因为第三方代码可能依赖于sys.path文档一致性:
As initialized upon program startup, the first item of this list,
path[0], is the directory containing the script that was used to
invoke the Python interpreter.
你混淆了追加和前置的概念。以下代码是前置代码:
1 | sys.path.insert(1,'/thePathToYourFolder/') |
它将新信息放在解释器将要经历的搜索序列的开头(嗯,第二,确切地说)。
建议您使用类似
python生态系统介绍
bootstrapping python虚拟环境
如果你决定走向环境隔离的道路,你肯定会受益于virtualenvwrapper:http://www.doughellmann.com/docs/virtualenvwrapper/