How to Create your own Custom Python Distribution Package?

  • Post author:
  • Post last modified:December 16, 2019
  • Post category:General
  • Reading time:6 mins read

Python programming language is one of the commonly used programming language in industry. With growing AI/ML, Python popularity is also growing. You may have to use many Python packages to build your application. Ever wondered how to create a Python distribution like Anaconda?. In this article, we will explain how to create your own Python package. You can later install package on the target machine.

How to Create your own Python Distribution Package?

There are many ways to package your Python application and create a setup.py to install the application on the target machine. But, there are very few methods that will allow you to create your own python distribution like Anaconda or Miniconda.

For example, your application may be using fewer Python modules and you don’t want to ask the customer to install a list of modules. You can create your own Python installer.

Let us discuss Conda constructor that will allow you to create a Python package will all required Python modules that your application may use.

Conda Constructor

Conda constructor is used to create cross-platform installer for a collection of conda packages. Basically, it creates an Anaconda-like installer consisting of conda packages. For example, your can create a installer.exe for Windows and Bash installer for Linux or Mac systems.

Install Conda Constructor

You conda to install constructor.

For examples, use below command.

conda install constructor
Create YAML file

Conda Constructor will take inputs from YAML file. Following is the constructor.yaml example.

name: MyOwnPython
version: 1.0.0

install_in_dependency_order: True

channels:
  - http://conda.anaconda.org/anaconda

specs:
  - python 3.7*
  - conda
  - numpy
  - pandas
  - scikit-learn

license_file: license.txt

You can provide more than one channel or even add local channel. In yaml file, you can also provide your license file that you want to add to installer.

You can also use conda-forge channel as it is community channel that has all the latest packages.

conda-forge channel : https://conda.anaconda.org/conda-forge/

Adding Local Channel to Conda Constructor

As mentioned earlier, you can make the local conda package visible to constructor by creating a specific sub-folder and index them using conda index command.

Conda Local Channel Sub directories

Channel/linux-64/package-1.0-0.tar.bz2
Channel/linux-32/package-1.0-0.tar.bz2
Channel/osx-64/package-1.0-0.tar.bz2
Channel/win-64/package-1.0-0.tar.bz2
Channel/win-32/package-1.0-0.tar.bz2

For example, below example creates the local channel that can be used in constructor.yaml file.

conda index D:\Work\Python_Packaging\Channel

Once the indexing is completed, you can use the following local file url to add local channels to yaml file.

file://D:\Work\Python_Packaging\Channel\ # [win]
file:///Work/Python_Packaging/Channel/ # [linux]
Create Python Distribution

Now, we have all the required files. Let us create the Python installer.

Create Python Installer for Windows

Firstly, let us create the windows installer (.exe).

Got to your constructor.yaml folder and execute the following command to create installer in the current directory.

D:\Work\Python_Packaging\MyOwnPython>constructor .

Once the constructor finishes, .exe file will be created.

MyOwnPython-1.0.0-Windows-x86_64.exe
Create Python Installer for Linux

Secondly, for a Linux, Bash installer will be created.

Create constructor.yaml, license.txt file and execute below command.

user1@server1-001:~/MyOwnPython$ constructor .

Once the constructor finishes, bash file will be created.

MyOwnPython-1.0.0-Linux-x86_64.sh

Related Articles

Set and use Environment variables inside Python Script

Hope this helps 🙂