9

https://www.python.org/dev/peps/pep-0503 and https://pip.pypa.io/en/stable/reference/pip_wheel/#cmdoption-i allude to being able to install python packages from a local directory, but it's not quite explicitly clear what this looks like in practice.

Do I use the same index.html files in my local directories? What does the argument to the --extra-index-url look like for a local directory?

1
  • To clarify, I want to be able to install using a requirements.txt, e.g. pip install -r requirements.txt Commented Aug 17, 2017 at 21:25

3 Answers 3

9

If you have a directory of distributions you want searched by pip, you may simply include the path to the directory:

pip install --extra-index-url=file:///path/to/wheelhouse somepackage

The /path/to/wheelhouse should to be structured like a simple repository, see PEP 503 – Simple Repository API. It is not necessary to run a webserver, serving from the filesystem is okay. A local directory of releases (e.g. .tar.gz and .whl files) can be made into the correct repository structure by running piprepo build /path/to/wheelhouse.

You could use --index-url instead of --extra-index-url if you don't want the remote PyPI searched at all. Note it's also possible to add --extra-index-url and/or --index-url at the top of your requirements.txt file.

Using pip, you can also install a distribution directly from a local file. For example, to install the copyingmock distribution:

$ curl https://pypi.python.org/packages/d9/26/5ae8945356634c87cdf099bd7cee57799df46798af90ae5ccb03961c6359/copyingmock-0.1-py2.py3-none-any.whl > copyingmock-0.1-py2.py3-none-any.whl
$ pip install ./copyingmock-0.1-py2.py3-none-any.whl

I've shown an example with a binary distribution, but the same should work for source distributions (.tar.gz).

Sign up to request clarification or add additional context in comments.

Comments

7

Let's say you have 2 packages you want to install from locally: abc-xyz and foo, and you have your corresponding package files abc-xzy-1.2.3.tar.gz and foo-1.0.0.tar.gz.

We'll put your local pypi directory at /my_local_pypi/simple

Your directory structure will look like:

/my_local_pypi/simple
  index.html
  - abc-xyz/
     index.html     
     abc-xyz-1.2.3.tar.gz  
  - foo/
     index.html
     foo-1.0.0.tar.gz

The root index.html needs <a href></a> anchor entries for each package, so should look like:

$ cat /my_local_pypi/simple/index.html
<!DOCTYPE html><html><body>
<a href="abc-xyz">abc-xyz></a></br>
<a href="foo">foo</a></br>
</body></html>

Then each $package/index.html needs an <a href></a> anchor pointing to the actual package file, so they should look like:

$ cat /my_local_pypi/simple/abc-xyz/index.html
<!DOCTYPE html><html><body>
<a href="abc-xyz-1.2.3.tar.gz">abc-xyz-1.2.3.tar.gz</a></br>
</body></html>

$ cat /my_local_pypi/simple/foo/index.html
<!DOCTYPE html><html><body>
<a href="foo-1.0.0.tar.gz">foo-1.0.0.tar.gz</a></br>
</body></html>

Then in your requirements.txt, you can do:

$ cat requirements.txt
--extra-index-url file:///my_local_pypi/simple/
abc-xyz==1.2.3
foo==1.0.0

And then you should be good to go: pip install -r requirements.txt

See also the piprepo project, which does a pretty good job of generating the local directory structure needed.

Comments

2

You won't have to use any index.html files.

Running the following should suffice:

pip install "path/to/file.whl"

This will install from local wheel file.

1 Comment

Can you use paths like that from a requirements.txt file also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.