Packaging Python Environment on Windows 10

Image for post
Image for post
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

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.

If you don’t already use Anaconda or Miniconda, download and install Miniconda (Miniconda3 Windows 64-bit) from here.

Image for post
Image for post
Miniconda Installation

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

Image for post
Image for post
Starting Miniconda from Windows 10 Search Box
Image for post
Image for post
Miniconda Prompt Window

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

conda create -n env python==3.8

Image for post
Image for post

You will be asked to if to proceed. Type “y”

Image for post
Image for post

After the “env” virtual environment is created, you need to activate it in order to use it by typing:

activate env

Image for post
Image for post

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

Image for post
Image for post

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 packing “env” virtual environment type the following command in Anaconda Prompt console window:

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

Assuming the virtual environment file is located at “c:\temp\env.tar.gz” and you want to unpackage it into the folder “c:\env”, type the following 2 commands in Windows’ console window (CMD window):

md c:\env

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

5. Activating an Unpackaged Python Environment

After unpacking 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):

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.

Written by

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