4

I'm new to python and was curious if python had something like an npm install that would pip install the required packages for a script I have. I've looked into the setup.py readme and it looks like its mostly geared to creating a tarball to send to pip, which isn't what I want.

I'd like to be able to check out the source code and then just run it. As it stands when I ask my coworkers to use the script they run into import failures and have to manually pip install things which is a poor experience.

My setup.py file is

#!/usr/bin/env python

from distutils.core import setup

setup(name='Add-Webhook',
      version='1.0',
      description='Adds webhooks to git repos',
      author='devshorts',
      packages=['requests'],
     )

And when I run it it

$ python setup.py install
running install
running build
running build_py
error: package directory 'requests' does not exist

I have a small script that sits next to the setup.py that uses the requests package and I'd like for it to be installed on 'install'

$ ls
total 40
-rw-r--r--  1 akropp  JOMAX\Domain Users  1039 Feb 24 09:51 README.md
-rwxr-xr-x  1 akropp  JOMAX\Domain Users  4489 Feb 27 17:01 add-webhook.py
-rw-r--r--  1 akropp  JOMAX\Domain Users   391 Feb 23 14:24 github.iml
-rw-r--r--  1 akropp  JOMAX\Domain Users   213 Apr  8 15:06 setup.py
devshorts
  • 8,572
  • 4
  • 50
  • 73
  • You can specify dependencies in a `setup.py`, if that's what you mean, then `python setup.py install` will install those too. Alternatively, if there's not too much installation required for your script, provide a [requirements file](http://pip.readthedocs.org/en/latest/user_guide.html#requirements-files) with it so they can more conveniently install everything in one go. – jonrsharpe Apr 08 '15 at 21:58
  • I put in the dependencies there but when I do `python setup.py` it complains that the module folders aren't there. I'd like for it to just install if its missing, something like the way npm does it? Not sure what I'm missing – devshorts Apr 08 '15 at 22:02
  • If your `setup.py` isn't working, perhaps you could share it, along with the complaints you're seeing? It's hard to provide helpful suggestions without seeing what you're working with; a [minimal example](http://stackoverflow.com/help/mcve) is always appreciated. – jonrsharpe Apr 08 '15 at 22:04

2 Answers2

6

Create requirements.txt file in your project's root directory, and add necessary Python packages with the versions you need.

Then just run $pip install -r requirements.txt to install everything that you have specified in requirements.txt file.

Not sure if this is what you need, but this is something better than running $pip install <package name> for several times.

hyunchel
  • 161
  • 2
  • 13
  • 1
    That works, is this the idiomatic way of dealing with package distribution in python? – devshorts Apr 08 '15 at 22:10
  • A solution without pip? It looks like the way to go is with source with the OP – Zizouz212 Apr 08 '15 at 22:11
  • 1
    Having `pip` replaced `easy_install` for installing python packages, yes. I'm sure there are other ways around, but this is the default method when dealing with python packages via pip. – hyunchel Apr 08 '15 at 22:12
  • 2
    @devshorts many packages you see on GitHub and elsewhere will have both `setup.py` and `requirements.txt` - often the former includes the minimal requirements for actually using the package, whereas the latter includes extra modules used for actual development (e.g. `pylint`, `sphinx`, ...). – jonrsharpe Apr 08 '15 at 22:21
  • 4
    @devshorts no problem. Note that if you've used a `virtualenv` to set up your development environment so that it only contains what's needed for this package, you can use `pip freeze > requirements.txt` to easily create the requirements file. – jonrsharpe Apr 08 '15 at 22:24
3

You have misunderstood the parameters for setup. The packages parameter is for specifying the packages that you are providing, not the dependencies of those packages.

Per the documentation:

Dependencies on other Python modules and packages can be specified by supplying the requires keyword argument to setup(). The value must be a list of strings. Each string specifies a package that is required, and optionally what versions are sufficient.

You could also consider using setuptools instead of distutils (switch to from setuptools import setup) and specifying install_requires (see the docs on dependency declarations) - see e.g. Differences between distribute, distutils, setuptools and distutils2?

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437