Skip to content
← Back to activity

Immutable solution version

Reference requirements.txt for the install_requires kwarg in setuptools setup.py file

Version 2Sign in to save

Complete solution

Reference requirements.txt for the install_requires kwarg in setuptools setup.py file: Update 12/2024: This does not work in recent versions of Python, primarily because the function has been moved around in the pip module.

Update 12/2024: This does not work in recent versions of Python, primarily because the function has been moved around in the pip module. Either way, as stated below by maintainers of the library, it is not recommended to import and use this function, as it is an internal function which is subject to change and being moved around. Old answer: You can flip it around and list the dependencies in setup.py and have a single character , a dot . , in requirements.txt instead. Alternatively, even if not advised, it is still possible to parse the requirements.txt file (if it doesn't refer any external requirements by URL) with the following hack (tested with pip 9.0.1 ): installreqs = parserequirements('requirements.txt', session='hack') This doesn't filter environment markers though. In old versions of pip, more specifically older than 6.0 , there is a public API that can be used to achieve this. A requirement file can contain comments ( # ) and can include some other files ( --requirement or -r ). Thus, if you really want to parse a requirements.txt you can use the pip parser: from pip.req import parserequirements # parserequirements() returns generator of pip.req.InstallRequirement objects installreqs = parserequirements([requirementspath]) # reqs is a list of requirement # e.g. ['django==1.5.1', 'mezzanine==1.4.6'] reqs = [str(ir.req) for ir in installreqs] setup( ... install_requires=reqs )

Context: I have a requirements.txt file that I'm using with Travis-CI. It seems silly to duplicate the requirements in both requirements.txt and setup.py , so I was hoping to pass a file handle to the installrequires kwarg in setuptools.setup . Is this possible? If so, how should I go about doing it? Here is my requirements.txt file: guessit>=0.5.2 tvdbapi>=1.8.2 hachoir-metadata>=1.3.3 hachoir-core>=1.3.3 hachoir-parser>=1.3.4

Matched source

Source: Source: https://stackoverflow.com/questions/14399534/reference-requirements-txt-for-the-install-requires-kwarg-in-setuptools-setup-py Original query: "Reference requirements.txt for the install_requires kwarg in setuptools setup.py file" Key terms: file, install, kwarg, reference, requirements, requires, setup, setuptools

Evidence

Source: I have a requirements.txt file that I'm using with Travis-CI. It seems silly to duplicate the requirements in both requirements.txt and setup.py , so I was hoping to pass a file handle to the installrequires kwarg in setuptools.setup . Is this possible? If so, how should I go about doing it? Here is my requirements.txt file: guessit>=0.5.2 tvdbapi>=1.8.2 hachoir-metadata>=1.3.3 hachoir-core>=1.3.3 hachoir-parser>=1.3.4

Verified resolution: Update 12/2024: This does not work in recent versions of Python, primarily because the function has been moved around in the pip module. Either way, as stated below by maintainers of the library, it is not recommended to import and use this function, as it is an internal function which is subject to change and being moved around. Old answer: You can flip it around and list the dependencies in setup.py and have a single character , a dot . , in requirements.txt instead. Alternatively, even if not advised, it is still possible to parse the requirements.txt file (if it doesn't refer any external requirements by URL) with the following hack (tested with pip 9.0.1 ): installreqs = parserequirements('requirements.txt', session='hack') This doesn't filter environment markers though. In old versions of pip, more specifically older than 6.0 , there is a public API that can be used to achieve this. A requirement file can contain comments ( # ) and can include some other files ( --requirement or -r ). Thus, if you really want to parse a requirements.txt you can use the pip parser: from pip.req import parserequirements # parserequirements() returns generator of pip.req.InstallRequirement objects installreqs = parserequirements([requirementspath]) # reqs is a list of requirement # e.g. ['django==1.5.1', 'mezzanine==1.4.6'] reqs = [str(ir.req) for ir in installreqs] setup( ... install_requires=reqs )

Source URL: https://stackoverflow.com/questions/14399534/reference-requirements-txt-for-the-install-requires-kwarg-in-setuptools-setup-py

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_U7ZCSo9j3JWq5a2cJ3LVkQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Reference requirements.txt for the install_requires kwarg in setuptools setup.py file <span>— Vectle</span></a>