【问题标题】:Replicate Python environment on another computer在另一台计算机上复制 Python 环境
【发布时间】:2021-05-12 17:26:01
【问题描述】:

如何将 windows 机器的 python 环境设置复制到另一台计算机上,并能够成功运行非常特定的脚本。

我们有在 anaconda 环境中使用 python 3.6.5 编写和运行的脚本,我们希望能够在新的 Windows 10 计算机上运行这些脚本。

脚本还连接到计算机上的本地数据库 (Postgres)。

【问题讨论】:

  • 这是个好问题。有一些技术可以在文件中克隆/保存环境状态,以便以后“复制”它。现实情况是,复制并不总是有效,尤其是在大型或旧环境中。目前还没有完整的解决方案,只能在各个地方找到最佳实践。

标签: python postgresql anaconda environment


【解决方案1】:

由于您使用的是 anaconda 环境,我假设您一直在为您提到的项目使用 virtualenv。使用以下代码实际上很容易复制:

# list all virtualenvs in your anaconda folder
$ conda info –envs          # this will list all virtualenvs created by you, you can then choose the specific virtualenv here.

# to activate the virtualenv of your interest
$ conda activate [virtualenv_name] 

# export all packages used in the specific virtualenv (conda activated) 
$ pip freeze > requirements.txt             # save the output file as requirements.txt

# set up a new conda virtualenv in current or separate machine and install with the requirements.txt
$ conda create --name <env_name> python=3.6.5 --file requirements.txt  

# Please note that occasionally you may need to check requirements.txt if there is any abnormal list of packages. The format should be in either [package==version] or [package].

或者你可以直接创建整个 virtualenv。

# copy exactly same virtualenv on separate machine

# export all packages used in the specific virtualenv (conda activated), including current python version and virtualenv name
$ conda env export > environment.yml        # save the output file as environment.yml    

# set up a new conda virtualenv in current or separate machine and install with the requirements.txt 
$ conda env create -f environment.yml       # using Conda; to modify “name” in the environment.yml file if to set up own same anaconda/machine

【讨论】:

  • 这个答案在很大程度上是正确的,请记住,您的原始环境可能安装了许多脚本实际上并不需要的包。使用此方法仍会在requirements.txt 中捕获这些内容,因此您安装的内容可能超出了要求。
猜你喜欢
  • 2021-11-27
  • 2015-09-08
  • 1970-01-01
  • 2021-03-02
  • 2016-04-16
  • 2014-04-22
  • 2021-09-01
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多