How to include library dependencies with a python project?

12,968

Solution 1

That's the topic of Python packaging tutorial.

In brief, you pass them as install_requires parameter to setuptools.setup().

You can then generate a package in many formats including wheel, egg, and even a Windows installer package.

Using the standard packaging infrastructure will also give you the benefit of easy version management.

Solution 2

You can build a package and specify dependencies in your setup.py. This is the right way

http://python-packaging.readthedocs.io/en/latest/dependencies.html

from setuptools import setup

setup(name='funniest',
  version='0.1',
  description='The funniest joke in the world',
  url='http://github.com/storborg/funniest',
  author='Flying Circus',
  author_email='[email protected]',
  license='MIT',
  packages=['funniest'],
  install_requires=[
      'markdown',
  ],
  zip_safe=False)

The need it now hack option is some lib that lets your script interact with shell. pexpect is my favorite for automating shell interactions.

https://pexpect.readthedocs.io/en/stable/

Solution 3

You should use virtualenv to manage packages of your project. If you do, you can then pip freeze > requirements.txt to save all dependencies of a project. After this requirements.txt will contain all necessary requirements to run your app. Add this file to your repository. All packages from requirements.txt can be install with

pip install -r requirements.txt

The other option will be to create a PyPI package. You can find a tutorial on this topic here

Share:
12,968

Related videos on Youtube

Matthew Winfield
Author by

Matthew Winfield

Updated on August 12, 2022

Comments

  • Matthew Winfield
    Matthew Winfield almost 2 years

    I'm working on a program which a few other people are going to be using, made in Python. They all have python installed, however, there are several libraries that this program imports from.

    I would ideally like to be able to simply send them the Python files and them be able to just run it, instead of having to tell them each of the libraries they have to install and as them to get each one manually using pip.

    Is there any way I can include all the libraries that my project uses with my Python files (or perhaps set up an installer that installs it for them)? Or do I just have to give them a full list of python libraries they must install and get them to do each one manually?

  • ivan_pozdeev
    ivan_pozdeev almost 7 years
    This requires the package to be available at PyPI, too.
  • wim
    wim almost 7 years
    requirements.txt is used for pinning versions. It's not for specifying dependencies, that's the job of setup.py.