Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include library dependencies with a python project?

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?

like image 561
Matthew Winfield Avatar asked Jul 12 '17 10:07

Matthew Winfield


People also ask

How do you add libraries to your program in Python?

A program must import a library module before using it.Use import to load a library module into a program's memory. Then refer to things from the module as module_name.

How would you manage dependencies in a Python project program?

Using venv and pipenv are two methods of managing dependencies in Python. They are simple to implement and, for most users, adequate solutions for handling multiple projects with different dependencies. However, they are not the only solutions. Other services can complement their use.

How do I see library dependencies in Python?

Pip Check Command – Check Python Dependencies After Installation. Because pip doesn't currently address dependency issues on installation, the pip check command option can be used to verify that dependencies have been installed properly in your project. For example: $ pip check No broken requirements found.


3 Answers

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.

like image 187
ivan_pozdeev Avatar answered Oct 24 '22 10:10

ivan_pozdeev


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/

like image 34
Xingzhou Liu Avatar answered Oct 24 '22 11:10

Xingzhou Liu


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

like image 33
pythad Avatar answered Oct 24 '22 11:10

pythad