Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "__requires__" mean in python?

I am newbie in python.

Could anybody answer what does __requires__ means in the following code? Why would they put __requires__ = 'flower==0.4.0' in the beginning of the file?

#!/srv/virtualenvs/zeusenv/bin/python

__requires__ = 'flower==0.4.0'
import sys
from pkg_resources import load_entry_point

sys.exit(
   load_entry_point('flower==0.4.0', 'console_scripts', 'flower')()
)
like image 954
user1859451 Avatar asked Dec 05 '12 09:12

user1859451


1 Answers

The __requires__ line is part of a generated console script. It has no meaning to Python itself, only the setuptools library uses this information.

Console scripts are python scripts defined in a python package metadata, and setuptools installs wrapper script files to let you run them as command line scripts. The flower file installed in your virtualenv is such a script, defined by the flower package setup.py file.

The pkg_resources module imported in the wrapper script inspects the value of __requires__ in the main script to make sure the correct version of the library is available and loaded before the load_entry_point function (or any other pkg_resources function) is run. It'll not install the version specified, it is assumed that that version is already installed on your system. It's purpose is to avoid loading incorrect, incompatible resources when the script runs and loads dependencies.

like image 82
Martijn Pieters Avatar answered Nov 16 '22 09:11

Martijn Pieters