Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify external system dependencies to a Python package?

When writing a Python package, I know how to specify other required Python packages in the setup.py file thanks to the field install_requires from setuptools.setup.

However, I do not know how to specify external system dependencies that are NOT Python packages, i.e. a commands such as git or cmake (examples) that my package could call via subprocess.call or subprocess.Popen?

Do I have to manually check the availability of the commands in my setup.py file, or is there a fancy way to specify system requirements?

Edit: I just want to be able to check if the external tools are available, and if not invite the user to install them (by themself). I do not want to manage the installation of external tools when installing the package.

Summary of contributions: it seems that setuptools has no support for this, and it would be safer to do the check at runtime (c.f. comments and answers).

like image 248
Odin Avatar asked Sep 22 '20 08:09

Odin


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.

Can Python modules have dependencies?

Dependency Hell Dependency conflicts occur when different Python packages have the same dependency, but depend on different and incompatible versions of that shared package. Because only a single version of a dependency is permitted in any project's environment, finding a compatible solution can be difficult.

How do I include dependencies in a Python project?

If your project has a dependency requirement that is not currently in the Python Package Index (PyPI), you can still include them if they can be accessed via http and are packaged as either an egg, .py file, or a VCS (Version Control System) repository, such as Git or Subversion.

What is dependency management in Python?

When managing Python environments, one of the key concerns is dependency management. Dependencies are all of the software components required by your project in order for it to work as intended and avoid runtime errors.

How do I install private dependencies in Python?

You can use the pip install command with the -t DIRECTORY flag to copy private dependencies into a local directory before deploying your app, as follows: Copy your dependency into a local directory: pip install -t DIRECTORY DEPENDENCY Add an empty __init__.py file to the DIRECTORY directory to turn it into a module.

How do I specify dependencies for Cloud Functions 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.


Video Answer


1 Answers

My recommendation would be to check for the presence of those external dependencies not at install-time but at run-time. Either at the start of each run, or maybe at the first run.

It's true that you could add this to your setup.py, but the setup.py is not always executed at install-time: for example if your project is packaged as a wheel then it doesn't even contain the setup.py file at all. And even if you do not distribute your project as a wheel, if I am not mistaken pip tends to build a wheel locally anyway and reuse it for the subsequent installations.

So although it would be possible to do such checks as part of the setup script at install time (provided that you can guarantee the presence and execution of setup.py), I would say run-time is a safer bet.

like image 129
sinoroc Avatar answered Oct 17 '22 10:10

sinoroc