为什么要使用sys.path.append(path)而不是sys.path.insert(1,path)? [英] Why use sys.path.append(path) instead of sys.path.insert(1, path)?

查看:355
本文介绍了为什么要使用sys.path.append(path)而不是sys.path.insert(1,path)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:根据Ulf Rompe的评论,请务必使用"1"而不是"0" ,否则将破坏

based on a Ulf Rompe's comment, it is important you use "1" instead of "0", otherwise you will break sys.path.

我已经使用Python已有一段时间了(一年多了),我一直很困惑为什么人们建议您使用sys.path.append()而不是sys.path.insert().让我演示一下.

I have been doing python for quite a while now (over a year), and I am always confused as to why people recommend you use sys.path.append() instead of sys.path.insert(). Let me demonstrate.

假设我正在开发一个名为PyWorkbooks的模块(该模块已安装在我的计算机上),但是同时我正在开发一个包含PyWorkbooks的模块(例如PyJob).在处理PyJob时,我发现正在纠正的PyWorkbooks中存在错误,因此我想导入开发版本.

Let's say I am working on a module named PyWorkbooks (that is installed on my computer), but I am simultaneously working on a different module (let's say PyJob) that incorporates PyWorkbooks. As I'm working on PyJob I find errors in PyWorkbooks that I am correcting, so I would like to import a development version.

有两种方法可以同时使用这两种方法(例如,我可以将PyWorkbooks项目放在PyJob内),但是有时我仍然需要尝试这条路径.但是,我不能简单地对PyWorkbooks所在的文件夹执行sys.path.append() .为什么?因为python会先找到我安装的PyWorkbooks!

There are multiple ways to work on both (I could put my PyWorkbooks project inside of PyJob, for instance), but sometimes I will still need to play with the path. However, I cannot simply do a sys.path.append() to the folder where PyWorkbooks is at. Why? Because python will find my installed PyWorkbooks first!

这就是为什么您必须执行sys.path.insert(1,path_to_dev_pyworkbooks)

This is why you have to do a sys.path.insert(1, path_to_dev_pyworkbooks)

总结:

sys.path.append(path_to_dev_pyworkbooks)
import PyWorkbooks # does NOT import dev pyworkbooks, imports installed one

或:

sys.path.insert(1, path_to_dev_pyworkbooks) # based on comments you should use **1 not 0**
import PyWorkbooks # imports correct file

这在过去给我造成了一些困扰,如果我们(作为一个社区)开始推荐sys.path.insert(1, path),我真的很喜欢,就像您手动插入路径一样,我认为可以肯定地说这就是您要使用的路径!

This has caused a few hangups for me in the past, and I would really like it if we (as a community) started recommending sys.path.insert(1, path), as if you are manually inserting a path I think it is safe to say that that is the path you want to use!

还是我有什么问题?这个问题有时会打扰我,我想在露天露面!

Or do I have something wrong? It's a question that sometimes bothers me and I wanted it in the open!

推荐答案

如果包/模块具有多个版本,则需要使用

If you have multiple versions of a package / module, you need to be using virtualenv (emphasis mine):

virtualenv是用于创建隔离的Python环境的工具.

virtualenv is a tool to create isolated Python environments.

要解决的基本问题是依赖项和版本之一以及间接权限. 假设您有一个需要使用LibFoo版本1的应用程序,而另一个应用程序则需要版本2.如果将所有内容都安装到/usr/lib/python2.7/site-packages中(或平台的标准位置是什么位置) ),很容易在无意中升级不应升级的应用程序的情况下结束.

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.

此外,如果无法将软件包安装到全局site-packages目录中怎么办?例如,在共享主机上.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

在所有这些情况下,virtualenv都可以为您提供帮助.它创建了一个具有自己的安装目录的环境,该环境不与其他virtualenv环境共享库(也可以选择不访问全局安装的库).

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

这就是为什么人们认为insert(0,是错误的-这是管理多个环境问题的不完整的权宜之计.

That's why people consider insert(0, to be wrong -- it's an incomplete, stopgap solution to the problem of managing multiple environments.

这篇关于为什么要使用sys.path.append(path)而不是sys.path.insert(1,path)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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