如何将 Python virtualenv 移动到不同的系统(计算机)并使用 Site-packages 中存在的包 [英] How to move Python virtualenv to different system (computer) and use packages present in Site-packages

查看:61
本文介绍了如何将 Python virtualenv 移动到不同的系统(计算机)并使用 Site-packages 中存在的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 python 3 应用程序(基于烧瓶),为此我在我的开发系统中创建了一个 virtualenv,通过 pip 安装了所有包,我的应用程序运行良好.

I am making a python 3 application (flask based) and for that I created a virtualenv in my development system, installed all packages via pip and my app worked fine.

但是当我将那个 virtualenv 移动到另一个系统(安装了 python3)并使用我的 virtualenv python 的绝对路径运行我的应用程序时 (c:/......./myenv/Scripts/python.exe main.py) 然后抛出未安装包的错误,我激活了 virtualenv 并使用了 pip freeze 并且没有安装任何软件包.

But when I moved that virtualenv to a different system (python3 installed) and ran my application with the absolute path of my virtualenv python (c:/......./myenv/Scripts/python.exe main.py) then it threw the errors that packages are not installed, I activated the virtualenv and used pip freeze and there were no packages were installed.

但是在 virtualenv 下有 'Site-Packages' (myenv -> lib -> site-packages) ,我所有安装的包都在那里.

我的问题是,即使在 Python 3 中将 virtualenv 移动到不同系统后,如何使用site-packages"中的包.

推荐答案

将 virtualenv 从一台计算机移动到另一台计算机,甚至在同一台计算机上从一个位置移动到另一个位置是一个坏主意,并且这就是为什么:

Moving a virtualenv from a computer to another, and even on the same computer from a location to another is a bad idea , and this is why :

  • 由于许多二进制文件和库都是符号链接,并且链接到您的旧系统二进制文件和库,因此在其他机器上无法运行.
  • 由于您的 virtualenv 中的许多 bin/ 脚本依赖于 系统 上的 virtualenv path,它如果您将 virtualenv 移动到另一个位置(即使在同一系统上),也将不起作用.
  • Since a lot of the binaries and libs are symlinks, and linked to your old system binaries and libs, it won't work on other machines.
  • Since many of bin/ scripts in your virtualenv depends on the virtualenv path on the system , it won't work if you moved the virtualenv to another location (even on same system either .)

所以推荐的方式是:

  • 首先生成requirements.txt文件:

  • First generate requirements.txt file :

 pip freeze > requirements.txt

  • 在移动所有内容(virtualenv 目录除外)后,创建一个新的 virtualenv,激活它并运行:

  • Second after moving everything (except the virtualenv directory) create a new virtualenv, activate it and run :

     pip install -r requirements.txt
    

  • 最后在你的情况下,如果你真的没有生成一个 requirements.txt 文件,并且需要使用旧的 site-packages ,有一个肮脏的解决方法我在 gnu/linux 机器上试过一次,但不知何故可以工作,但我不能 100% 确定它是否能正常工作,所以如果你想试一试.

    Finally in your case if you really didn't generated a requirements.txt file, and need to use the old site-packages , there is a dirty workaround which i tried once on a gnu/linux machine and somehow worked but am not 100% sure if it will work properly so if you want give it a try.

    • site-packages 复制到 your-old-virtualenv/lib/python{version}/ 中的某个地方,例如桌面
    • 删除旧的virtualenv,并创建一个新的virtualenv
    • new-virtualenv/lib/python{version}new virtualenv 中的 site-packages 替换为 old 站点包
    • 删除新复制的site-packages
    • 中的__pycache__文件夹
    • 激活新的 virtualenv 并测试是否一切正常.
    • copy the site-packages in your-old-virtualenv/lib/python{version}/ somewhere in your new computer , Desktop for example
    • Delete the old virtualenv, and create a new virtualenv
    • Replace the site-packages in the new virtualenv in new-virtualenv/lib/python{version} with the old site-packages
    • Delete __pycache__ folder in the newly copied site-packages
    • Activate the new virtualenv and test if everything is working .

    注意您应该使用相同的python版本 2 或 3 ,不要期望依赖于 python2 的 virtualenv 与 python3 一起正常运行

    Note that you should use the same python version either 2 or 3 , don't expect a virtualenv that depends on python2 to run properly with python3

    这篇关于如何将 Python virtualenv 移动到不同的系统(计算机)并使用 Site-packages 中存在的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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