This tutorial describes how to transfer a Python environment from one Windows 10 computer into another Windows 10 computer. The packaged Python environment which is created this way, can be used on any Windows 10 computer where python isn’t already installed and can be used for building an installation file for a Python application
1. Create a Local Virtual Environment with Conda
This tutorial requires that you use Anaconda or Miniconda Python distribution.
Anaconda is a free and open-source distribution of the Python programming language (and also R programming language) for scientific computing, that aims to simplify package management and deployment. Miniconda is a smaller version of Anaconda which includes a smaller number of packages. Conda is the component of Anaconda and Miniconda which is responsible for package management and deployment.
Running Anaconda Prompt
If you don’t already use Anaconda or Miniconda, download and install Miniconda (Miniconda3 Windows 64-bit) from here.
After installing Miniconda (or Anaconda), open “Anaconda Prompt (Python)” from Windows 10 search box.
Create A New Virtual Environment
A virtual environment is a named, isolated, working copy of Python that maintains its own files, directories, and paths so that you can work with specific versions of libraries or Python itself without affecting other Python projects. Virtual environments make it easy to cleanly separate different projects and avoid problems with different dependencies and version requirements across components.
Firstly, decide which name do you want to give to your virtual environment.
Assuming you chose for your virtual environment name “env”, in the Anaconda Prompt console windows type: (replace “env” with the name you chose):
You will be asked to if to proceed. Type “y”
After the “env” virtual environment is created, you need to activate it in order to use it by typing:
You can make sure that “env” virtual environment is active according the (env) label before the current prompt line.
While the virtual environment is active, usually you can install any Python package by typing:
Where package-name is the name of the package.
But sometimes it is not clear what is the right package-name for a specific import and sometimes some other command line arguments are required for a specific package. So, if you are not sure how to install some package try to search in Google “conda install package-name” and you will immediately find the right command for installing it.
If you have some Python script (let’s say “main.py”) in your current directory, you can try running it by typing:
After any try of running it, in which it tells you that some import is missing, you can install the missing package using a “conda install” command.
Once you finish using the virtual environment you can get out of it by typing:
2. Install Conda-Pack
We will use Conda-Pack for packaging the Conda Virtual Environment into a file. This tool is required only for packaging the Virtual Environment. For unpackaging the Virtual Environment this tool is not required.
conda-pack is a command line tool for creating relocatable conda environments. This is useful for deploying code in a consistent environment, potentially in a location where python/conda isn’t already installed.https://conda.github.io/conda-pack/
The simplest method for installing this tool, in my opinion, is installing from code.
Type in Anaconda Prompt window the following command:
pip install git+https://github.com/conda/conda-pack.git
3. Packaging the Python Environment Into a File
For packaging “env” virtual environment type the following command in Anaconda Prompt console window:
After running this command, “env” virtual environment will be packaged into a file named “env.tar.gz”
In some cases conda pack can not find the location of the virtual environment. In such cases use the “-p” argument instead of “-n” and write the environment full path instead of env. In order to find the virtual environment full path:
- Activate the virtual environment by typing:
- Exit the virtual environment by typing:
4. Unpackaging the Python Environment from a File
Assuming the virtual environment file is located at “c:\temp\env.tar.gz” and you want to unpack it into the folder “c:\env”, type the following 2 command in Windows’ console window (CMD window):
5. Activating an Unpackaged Python Environment
After unpackaging a Python virtual environment from a file into a folder, let’s say into folder “c:\env”, activate it by typing the following command in Window console window (CMD window):
I hope that this tutorial did help. Please let me know if you find any mistake.