use a relative path in requirements.txt to install a tar.gz file with pip

PythonPip

Python Problem Overview


We're using a requirements.txt file to store all the external modules needed. Every module but one is gathered from internet. The other one is stored on a folder under the one holding the requirements.txt file.

BTW, this module can be easily installed with pip install

I've tried using this:

file:folder/module

or this:

file:./folder/module

or even this:

folder/module

but always throws me an error. Does anyone know which is the right way to do this?

Thanks

Python Solutions


Solution 1 - Python

In the current version of pip (1.2.1) the way relative paths in a requirements file are interpreted is ambiguous and semi-broken. There is an open issue on the pip repository which explains the various problems and ambiguities in greater detail:

https://github.com/pypa/pip/issues/328

Long story short the current implementation does not match the description in the pip documentation, so as of this writing there is no consistent and reliable way to use relative paths in requirements.txt.

THAT SAID, placing the following in my requirements.txt:

./foo/bar/mymodule

works when there is a setup.py at the top level of the mymodule directory. Note the lack of the file:: protocol designation and the inclusion of the leading ./. This path is not relative to the requirements.txt file, but rather to the current working directory. Therefore it is necessary to navigate into the same directory as the requirements.txt and then run the command:

pip install -r requirements.txt

Solution 2 - Python

Its based off the current working directory (find with os.getcwd() if needed) and the relative path you provide in the requirements file.

Your requirements file should look like this:

fabric==1.13.1
./some_fir/some_package.whl
packaging==16.8

Note this will only work for .whl files not .exe

Remember to keep an eye on the pip install output for errors.

Solution 3 - Python

For me only the file: directive worked. This even works with AWS SAM, i.e. sam build. Here is my requirements.txt and the englishapps is my own custom Python package that I need in AWS Lambda

requests
file:englishapps-0.0.1-py3-none-any.whl

Solution 4 - Python

As was mentioned before, the files are relative to the current working directory, not the requirement.txt.

Since v10.0 requirements files support environment variables in the format: ${VAR_NAME}. This could be used as a mechanism to specify a file location relative to the requirements.txt. For example:

# Set REQUIREMENTS_DIRECTORY outside of pip
${REQUIREMENTS_DIRECTORY}/folder/module

Solution 5 - Python

Another option is to use the environment manager called Pipenv to manage this use case. The steps after you do the pipenv install for a new project:

pipenv install -e app/deps/fastai (-e is editable, and is optional)

then you will see the following line in your Pipfile:
fastai = {editable = true,path = "./app/deps/fastai"}

here's similar issues:

https://github.com/pypa/pipenv/issues/209#issuecomment-337409290 https://stackoverflow.com/a/53507226/7032846

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionF.D.F.View Question on Stackoverflow
Solution 1 - PythonfinnView Answer on Stackoverflow
Solution 2 - PythonfrageView Answer on Stackoverflow
Solution 3 - PythonEdgar ManukyanView Answer on Stackoverflow
Solution 4 - PythonTim LudwinskiView Answer on Stackoverflow
Solution 5 - PythonAaron SoellingerView Answer on Stackoverflow