Packaging Python Environment on Windows 10

Photo by Christian Fregnan

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

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

Miniconda Installation

After installing Miniconda (or Anaconda), open “Anaconda Prompt (Python)” from Windows 10 search box.

Starting Miniconda from Windows 10 Search Box
Miniconda Prompt Window

Create A New Virtual Environment

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 have chosen):

conda create -n env python==3.8

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:

activate env

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:

conda install package-name

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:

python main.py

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:

deactivate

2. Install Conda-Pack

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

conda pack -n env

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:

  1. Activate the virtual environment by typing: activate env
  2. Type: where python
  3. Exit the virtual environment by typing: deactivate

4. Unpackaging the Python Environment from a File

md c:\env

tar -xzf c:\temp\env.tar.gz -C c:\env

5. Activating an Unpackaged Python Environment

c:\env\script\activate

I hope that this tutorial did help. Please let me know if you find any mistake.

Originally published at https://www.orensifri.meddevsoft.com.

Software developer & architect. CTO at MedDev Soft

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store