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

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

问题描述

根据 Ulf Rompe 的评论,使用1"而不是0"很重要,否则会破坏 sys.path.

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!

推荐答案

如果你有多个版本的包/模块,你需要使用 virtualenv(强调我的):

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.

正在解决的基本问题是依赖项和版本之一,以及间接权限.假设您有一个应用程序需要版本 1 的 LibFoo,但另一个应用程序需要版本 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天全站免登陆