在另一台计算机上复制 Python 环境

Replicate Python environment on another computer

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

我们在 anaconda 环境中 python 3.6.5 中编写了 运行 脚本,我们希望能够 运行 这些脚本在新的 Windows 10台电脑。

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

既然你使用的是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