Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do install packages from a local python package index?

Tags:

python

pip

pypi

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?

like image 614
Mike Bjorge Avatar asked Aug 17 '17 21:08

Mike Bjorge


People also ask

How do I install a Python package from local?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

How do I install an external package in Python?

The command to install any external Python package is pip install. The pip version can be checked using pip --version or pip -V. If the path shows Python 2.7, then make sure you have Python version 3 installed and then run pip as pip3.


2 Answers

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.

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).

like image 99
wim Avatar answered Oct 12 '22 04:10

wim


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.

like image 28
Mike Bjorge Avatar answered Oct 12 '22 04:10

Mike Bjorge