Setting Up setup.py with Dependencies Installed by Conda

In the world of Python, setup.py is a well-known tool for distributing Python packages. However, when it comes to managing dependencies, pip is often the go-to choice. But what if you want to use conda instead? This blog post will guide you through the process of setting up setup.py with dependencies installed by conda, a package manager that can handle libraries outside of Python and manage environments.

Setting Up setup.py with Dependencies Installed by Conda

In the world of Python, setup.py is a well-known tool for distributing Python packages. However, when it comes to managing dependencies, pip is often the go-to choice. But what if you want to use conda instead? This blog post will guide you through the process of setting up setup.py with dependencies installed by conda, a package manager that can handle libraries outside of Python and manage environments.

Why Conda?

Before we dive into the how, let’s briefly discuss the why. Conda is a cross-platform package manager that can install packages for multiple languages, not just Python. It’s particularly useful for data scientists who often need to work with packages that have complex dependencies, such as NumPy, SciPy, and TensorFlow.

Conda also allows you to create isolated environments, which can be very useful when working on multiple projects with different dependencies.

Step 1: Creating a Conda Environment

First, we need to create a new conda environment. This can be done using the conda create command. For instance, to create an environment named myenv, you would use:

conda create --name myenv

Step 2: Activating the Environment

Once the environment is created, you can activate it using the conda activate command:

conda activate myenv

Step 3: Installing Dependencies

Next, we need to install the dependencies. This can be done using the conda install command. For example, to install numpy, you would use:

conda install numpy

Step 4: Creating setup.py

Now, let’s move on to creating the setup.py file. This file is used to distribute Python packages and it’s where we specify our dependencies. Here’s a basic example:

from setuptools import setup

setup(
    name='my_package',
    version='0.1',
    description='A sample Python package',
    install_requires=[
        'numpy',
    ],
)

In this example, numpy is listed as a dependency in the install_requires section.

Step 5: Using Conda to Install Dependencies

Here’s where things get a bit tricky. By default, setup.py uses pip to install dependencies. However, we can override this by creating a post-install script that uses conda instead.

First, we need to create a requirements.txt file that lists our dependencies:

numpy

Then, we can create a post-install script:

#!/bin/bash

while read requirement; do conda install --yes $requirement; done < requirements.txt

Finally, we can modify our setup.py file to run this script after installation:

from setuptools import setup
from setuptools.command.install import install

class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        install.run(self)
        # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION

setup(
    name='my_package',
    version='0.1',
    description='A sample Python package',
    install_requires=[
        'numpy',
    ],
    cmdclass={
        'install': PostInstallCommand,
    }
)

In this modified setup.py file, we’ve added a PostInstallCommand class that overrides the run method of the install command. This method is called after the package is installed, and we use it to run our post-install script.

Conclusion

While pip is the default package manager for Python, conda offers several advantages, especially for data scientists working with complex dependencies. By following the steps outlined in this post, you can set up setup.py to use conda to install dependencies, allowing you to leverage the power of conda in your Python package distribution.

Remember, the key to successful Python package distribution is managing dependencies effectively. Whether you choose pip or conda, understanding how to use your package manager of choice is essential.

Tags: Python, Conda, setup.py, Data Science, Dependencies

Meta Description: Learn how to set up setup.py with dependencies installed by conda, a powerful package manager for Python that’s particularly useful for data scientists.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.