40

I have a script that creates a virtualenv, installs distribute and pip in it and then optionally clones a git repo.

Now I have the project I will be working on, installed. But its dependencies are not installed. How can I make pip install all the dependencies as if I have issued a pip install MyApp?

EDIT: Appareantly my question is a duplicate of this one.

Not exactly sure but pip install -e . seems to do what I want without too many extra stuff lying around. I'd prefer if my code wasn't linked from site-packages though.

pjs
  • 17,381
  • 4
  • 25
  • 47
muhuk
  • 15,287
  • 9
  • 55
  • 96
  • 3
    *I have a script that creates a virtualenv, installs distribute and pip in it (...)*. You know you can do all 3 things with `pip -E VENV_DIR pip` as pip by default installs pip and distribute in the newly created environment? – Piotr Dobrogost Nov 06 '11 at 22:04
  • Cool tip! I don't use the mentioned script anymore though. – muhuk Nov 14 '11 at 01:54

4 Answers4

22

If your dependencies are defined in the setup.py file, you can first dump them to an external file using:

python setup.py egg_info

This will list all your dependencies in YOUR_PROJECT.egg-info/requires.txt file. Then you can install them using pip:

pip install -r *.egg-info/requires.txt
Jakub Kukul
  • 9,209
  • 2
  • 41
  • 48
16

In my package root issuing pip install -e . installs dependencies.

muhuk
  • 15,287
  • 9
  • 55
  • 96
  • 18
    That's some pretty serious side-effecting. `-e` installs in editable mode, which means the package gets linked from site packages instead of copied. I'd expect this approach to cause weird and subtle problems when you go and try to install the package for real, especially if you don't `pip uninstall` it first. – Silas Ray Jun 30 '16 at 15:36
  • 2
    Please note that it works only for setup.py based builds for now, there is no support for pyproject.toml yet. – maciek Feb 22 '19 at 15:30
  • Note that this tries to install the package itself too in some way. See https://github.com/pypa/pip/issues/7218 for an example where this has unwanted effects. – bli Oct 15 '19 at 15:26
  • 2
    this is obviously not the right answer! Why is this accepted? This will do much more than "only install dependencies". – Charlie Parker Sep 23 '21 at 16:41
3

You should use the pip requirements file.

Essentially, place all your requirements, one in each line in a file and pass that to pip using the command

pip install -r requirements.txt

What more, if you have a standard environment, pip can actually dump such a file from existing installs using the command:

pip freeze

You can put the file thus generated directly into the pip requirements, and call the previous command from your deployment script.

Pretty cool, isnt it? :)

lprsd
  • 80,809
  • 47
  • 132
  • 167
  • 14
    Cool, but it doesn't answer my question. I'm not looking for a way to define dependencies. Reading questions entirely and carefully helps goes a long way in providing correct answers. – muhuk Feb 23 '10 at 18:08
  • 9
    Wait, You can put all your dependencies in a file and ask pip to install them all for you. Isn't that what you are looking for? If not, I didn't properly understand your question. Even now. – lprsd Feb 24 '10 at 10:31
  • 2
    the difference being where the 'dependencies' are specified - in the requirements.txt (where you would specify "myrepo==1.0.1" or whatever) OR in the setup.py of the package in myrepo that you want to install. if you've already got the repo in hand then only installing the dependencies might make sense. – andy May 03 '14 at 22:12
  • @Tgr I don't think that is correct. I just used pip3 to install via a requirements file and several transitive dependencies were installed. – dantiston Mar 26 '17 at 03:24
  • @dantiston this definitely [used to be a problem](http://stackoverflow.com/questions/28110449/can-pip-install-packages-recursively-from-requirements-txt) some time ago. I have run into it with a recent version of pip as well, but it's entirely possible that that was some kind of user error on my side. – Tgr Mar 26 '17 at 04:22
  • 2
    The question is asking for a way to install the dependencies of *the package being developed*. Its dependencies will be declared in the package's `setup.py` file. It could even have conditional dependencies (like based on OS). I don't think requirements.txt can handle that, but even if it can, you'd have to do some extra work to load the requirements into `setup.py` from the file. – jpmc26 May 02 '19 at 22:24
  • 1
    This is not quite right, because `setup.py` and `requirements.txt` are for different purposes and their roles are not interchangeable. See Python packaging document: https://packaging.python.org/discussions/install-requires-vs-requirements/#requirements-files – xuhdev Dec 19 '20 at 20:03
3

To install your project's dependencies (i.e. install_requires + extra_requires) you have to extract your dependencies using setuptools egg-info and then install the filtered list of the combined dependencies:

python setup.py egg_info
pip install `grep -v '^\[' *.egg-info/requires.txt`
jfaleiro
  • 96
  • 4