Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple dependency management for a Python project

I am coming from Java background and completely new at Python.

Now I have got a small project with a few Python files that contain a few imports. I know I does not have the imported dependencies installed on my computer, so I try to figure out the required dependencies and run pip to install them.

I would like to do it differently. I would prefer to have the dependencies listed in a single file and install them automatically during the build process.

Does it make sense ? If it does I have a few questions:

  • How to list the project dependencies required to install by pip ?
  • How to run pip to install the dependencies from the list ?
like image 509
Michael Avatar asked May 07 '17 07:05

Michael


People also ask

How do I set dependencies in Python?

There are two ways to specify dependencies for Cloud Functions written in Python: using the pip package manager's requirements. txt file or packaging local dependencies alongside your function. Dependency specification using the Pipfile/Pipfile.

What are dependencies in Python project?

Dependencies are all of the software components required by your project in order for it to work as intended and avoid runtime errors. You can count on PyPI (the Python Package Index) to provide packages that can help you get started on everything from data manipulation to machine learning to web development, and more.

Is pip a dependency management tool?

Pip comes with Python versions later than 2.7. 9, and is the default go-to packaging tool. However, its dependency management capabilities are pretty basic, and its speed is considered slow.


1 Answers

A common way to manage dependencies for a python project is via a file in root of the project named "requirements.txt". An easy way to make this is:

  1. Setup a python virtualenv for your project
  2. Manually install the required modules via pip
  3. Execute pip freeze > requirements.txt to generate the requirements file

You can then install all the dependencies in other locations using pip install -r requirements.txt.

If you want dependencies to be installed automatically when other people pip install your package, you can use install_requires() in your setup.py.

like image 197
jordanm Avatar answered Oct 25 '22 19:10

jordanm