zjffdu
zjffdu

Reputation: 28734

How to package python program with its depdent packages?

I want to package my Python program with its dependent packages and deploy it. The reason why I also want to include its dependencies is that the target machine may have another version of the dependent library, and I want to use a specific version. I am not sure whether python has dependency management tool. I think dependency management tool like maven of java is what I want.

Upvotes: 1

Views: 59

Answers (1)

Mike Müller
Mike Müller

Reputation: 85432

Make it pip-installable

There is extensive documentation on how to create a pip-instable package.

The main thing is to write a setup.py:

from setuptools import setup

setup(name='my_package_name',
      version='0.1',
      description='It solves all your problems',
      url='http:solves.it',
      author='Me',
      author_email='[email protected]',
      license='MIT',
      packages=['my_package_name'],
      install_requires=[
          'flask',
          'another_package'
      ],
      zip_safe=False)

Here install_requires contains the list of dependencies. You can also use a an requirements.txt file to list all the dependencies with version number.

You can create such file with:

pip freeze > requirements.txt

Than you can re-create the same environment with:

pip install -r requirements.txt

Make it standalone

Probably the most powerful, cross-platform library for this is PyInstaller. It works for Python 2 and 3 alike, allows to distribute your program as a single executable file or a single directory, and supports many commonly used Python libraries.

Upvotes: 1

Related Questions