Python 将完整的 virtualenv 复制到另一台电脑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34993200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:51:19  来源:igfitidea点击:

Copy complete virtualenv to another pc

pythoncopypipvirtualenv

提问by vicco

I have a virtualenvlocated at /home/user/virtualenvs/Environment. Now I need this environment at another PC. So I installed virtualenv-cloneand used it to clone /Environment. Then I copied it to the other PC via USB. I can activate it with source activate, but when I try to start the python interpreter with sudo ./Environment/bin/pythonI get

我有一个virtualenv位于/home/user/virtualenvs/Environment. 现在我在另一台 PC 上需要这个环境。所以我安装virtualenv-clone并用它来克隆/Environment. 然后我通过USB将其复制到另一台PC。我可以用 激活它source activate,但是当我尝试用 启动 python 解释器时,sudo ./Environment/bin/python我得到

./bin/python: 1: ./bin/python: Syntax Error: "(" unexpected

Executing it without sudo gives me an error telling me that there is an error in the binaries format. But how can this be? I just copied it. Or is there a better way to do this? I can not just use pip freezebecause there are some packages in /Environment/lib/python2.7/site-packages/which I wrote myself and I need to copy them, too. As I understand it pip freezejust creates a list of packages which pip then downloads and installs.

在没有 sudo 的情况下执行它会给我一个错误,告诉我二进制格式有错误。但这怎么可能?我刚刚复制了它。或者有没有更好的方法来做到这一点?我不能只是使用,pip freeze因为有一些包是/Environment/lib/python2.7/site-packages/我自己编写的,我也需要复制它们。据我了解,它pip freeze只是创建了一个包列表,然后 pip 会下载和安装这些包。

回答by Anuj Gupta

Do the following steps on the source machine:

在源机器上执行以下步骤:

  1. workon [environment_name]
  2. pip freeze > requirements.txt
  3. copy requirements.txt to other PC
  1. 工作 [environment_name]
  2. pip冻结>需求.txt
  3. 将requirements.txt复制到其他PC

On the other PC:

在另一台电脑上:

  1. create a virtual environment using mkvirtualenv [environment_name]
  2. workon [environment_name]
  3. pip install -r requirements.txt
  1. 使用 mkvirtualenv [environment_name] 创建虚拟环境
  2. 工作 [environment_name]
  3. pip install -r requirements.txt

You should be done.

你应该完成。

Other Resources:

其他资源:

回答by Lerner Zhang

I think what occurs is that you just copy the symbolic links in the source file to the target machine as binary files(no longer links). You should copy it using rsync -lto copy to keep those links.

我认为发生的情况是您只需将源文件中的符号链接作为二进制文件(不再是链接)复制到目标机器。您应该使用rsync -lto copy 来复制它以保留这些链接。

回答by Mueladavc

Usually I use virtualenv to create a new environment, then I go to the environment where I want to copy from, copy all the folders and paste it into the environment folder I just created, but most importantly when asking if you want to replace the Destination files, choose to skip these files. This way you keep your settings. At least for me, this has worked very well. I hope it works for you too.

通常我使用virtualenv新建一个环境,然后我去我要复制的环境,把所有的文件夹复制粘贴到我刚创建的环境文件夹中,但最重要的是在询问是否要替换Destination时文件,选择跳过这些文件。这样您就可以保留您的设置。至少对我来说,这非常有效。我希望它也适用于你。

回答by Carson

I share my experience.

我分享我的经验。

Suppose another PC does not install Python
Python version: 3.7.3 
Platform: Platform: Windows 10, 7 (64bit)

The following is working for me.

以下是为我工作。

Step:

步:

  1. download Windows embeddable zip file
  2. download get-pip.py(because the embeddable zip filedoes not provide pip)
  3. [Optional] install tkinter, see this article: Python embeddable zip: install Tkinter
  4. Choose a packaging method (I use NSIS: https://nsis.sourceforge.io/Download)
  1. 下载Windows 可嵌入的 zip 文件
  2. 下载get-pip.py(因为embeddable zip file不提供pip)
  3. 【可选】安装tkinter,看这篇文章:Python embeddable zip: install Tkinter
  4. 选择一种打包方式(我使用 NSIS:https: //nsis.sourceforge.io/Download

folder artictures:

文件夹图:

- main.nsi
- InstallData
    - contains: Step1 & Step2
- YourSitePackages  # I mean that packages you do not intend to publish to PyPI.
    - LICENSE
    - MANIFEST.in
    - README.rst
    - ...
    - requirements.txt
    - setup.py

The abbreviated content about main.nsi is as follows:

main.nsi 的简写内容如下:

!define InstallDirPath "$PROGRAMFILES\ENV_PYTHON37_X64"
!define EnvScriptsPath "${InstallDirPath}\Scripts"
...

CreateDirectory "${InstallDirPath}"  # Make sure the directory exists before the writing of Uninstaller. Otherwise, it may not write correctly!
SetOutPath "${InstallDirPath}"
SetOverwrite on
File /nonfatal /r "InstallData\*.*"

SetOutPath "${InstallDirPath}\temp"
SetOverwrite on
File /nonfatal /r "YourSitePackages\*.*"
nsExec::ExecToStack '"${InstallDirPath}\python.exe" "${InstallDirPath}\get-pip.py"'  # install pip
nsExec::ExecToStack '"${InstallDirPath}\Scripts\pip.exe" install "${InstallDirPath}\temp\."'  # install you library. same as: `pip install .`
RMDir /r "${InstallDirPath}\temp"  # remove source folder.
...

/*
Push ${EnvScriptsPath}  # Be Careful about the length of the HKLM.Path. it is recommended to write it to the HKCU.Path, it is difficult for the user path to exceed the length limit
Call AddToPath  # https://nsis.sourceforge.io/Path_Manipulation
*/

hope someone will benefit from this.

希望有人会从中受益。