简体   繁体   English

将Python环境移至新的OS安装

[英]Moving a Python environment over to a new OS install

I have reinstalled my operating system (moved from windows XP to Windows 7). 我重新安装了我的操作系统(从Windows XP迁移到Windows 7)。 I have reinstalled Python 2.7. 我重新安装了Python 2.7。

But i had a lot of packages installed in my old environment. 但我在旧环境中安装了很多软件包。 (Django, sciPy, jinja2, matplotlib, numpy, networkx, to name just a view) (Django,sciPy,jinja2,matplotlib,numpy,networkx,仅举一个视图)

I still have my old Python installation lying around on a data partition, so i wondered if i can just copy-paste the old Python library folders onto the new installation? 我仍然在数据分区上放置旧的Python安装,所以我想知道我是否可以将旧的Python库文件夹复制粘贴到新安装中? Or do i need to reinstall every package? 或者我需要重新安装每个包吗?

Do the packages keep any information in registry, system variables or similar? 包是否在注册表,系统变量或类似信息中保留任何信息?

Does it depend on the package? 它取决于包装吗?

That's the point where you must be able to layout your project, thus having special tools for that. 这就是您必须能够布置项目的地方,因此需要使用特殊工具。

Normally, Python packages do not do such wierd things as dealing with registry (unless they are packaged via MSI installer). 通常情况下,Python包不会像处理注册表那样做很奇怪的事情(除非它们是通过MSI安装程序打包的)。 The problems may start with packages that contain C extensions, so moving to another version of OS or from 32 to 64-bit architecture will require recompiling/rebuilding of those. 问题可能从包含C扩展的包开始,因此转移到另一个版本的OS或从32位到64位架构将需要重新编译/重建这些。 So, it would be much better to reinstall all packages to new system as written below. 因此,如下所示,将所有软件包重新安装到新系统会好得多。

Your demands may vary, but you definitely must choose the way of building your environment. 您的要求可能会有所不同,但您必须选择构建环境的方式。 If you don't have and plan to have a large variety of projects you may consider the first approach as follows below, the second approach is more likely for setting up development environment for different projects or for different versions of the same project. 如果您没有并计划拥有大量项目,您可以考虑采用以下第一种方法,第二种方法更有可能为不同项目或同一项目的不同版本设置开发环境。

  1. Global environment (your Python installation in your system along with installed packages). 全局环境 (系统中的Python安装以及已安装的软件包)。

    Here you can consider using pip . 在这里你可以考虑使用pip In this case your project can have requirements file containing all needed packages for your project. 在这种情况下,您的项目可以包含包含项目所需的所有包的需求文件。 Basically, requirements file is a text file containing package names (on PyPI and their versions). 基本上,需求文件是包含包名称的文本文件(在PyPI及其版本上)。

  2. Isolated environment . 孤立的环境 It can be achieved using special tools or specially organized path. 它可以使用特殊工具或特殊组织的路径来实现。

    Here where pip can be gracefully combined with virtualenv . 在这里,pip可以与virtualenv优雅地结合在一起。 This way is highly recommended by a lot of developers (I must remind that Python 3.3 that will soon be released contains virtualenv as a part of standard library). 许多开发人员强烈推荐这种方式(我必须提醒即将发布的Python 3.3包含virtualenv作为标准库的一部分)。 This approach assumes creating virtual shell with its own instance of Python interpreter and installed packages. 此方法假定使用自己的Python解释器实例和已安装的包创建虚拟shell。

    Another popular tool for achieving isolated environment is called buildout. 另一种实现隔离环境的流行工具称为buildout。 It lays out your project source and dependencies in one path so you achieve the same effect as virtualenv creates. 它在一个路径中列出您的项目源和依赖项,以便您实现与virtualenv创建的相同的效果。 The great advantage of buildout that it's built upon an idea of pluggable recipes (pieces of code implementing different common project deployment tasks) and there are hundreds of stable and reliable recipes over Internet. buildout的巨大优势在于它基于可插拔配方(实现不同的常见项目部署任务的代码片段)的构思,并且在Internet上有数百个稳定可靠的配方。

    Both virtualenv and buildout help you to remove head-ache when installing dependencies and solve the problem of different versions of the same package kept on a single machine. virtualenv和buildout都可以帮助您在安装依赖项时消除头痛,并解决在一台机器上保留的同一软件包的不同版本的问题。

Choose your destiny... 选择你的命运...

The short answer to this question is "no", since packages can execute arbitrary code on installation and do whatever the heck they want wherever they want on your system. 对这个问题的简短回答是“不”,因为软件包可以在安装时执行任意代码,并在系统上的任何地方做任何他们想要的事情。

Just reinstall all of them. 只需重新安装所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM