144

python setup.py install will automatically install packages listed in requires=[] using easy_install. How do I get it to use pip instead?

joeforker
  • 36,731
  • 34
  • 138
  • 231

3 Answers3

147

Yes you can. You can install a package from a tarball or a folder, on the web or your computer. For example:

Install from tarball on web

pip install https://pypi.python.org/packages/source/r/requests/requests-2.3.0.tar.gz

Install from local tarball

wget https://pypi.python.org/packages/source/r/requests/requests-2.3.0.tar.gz
pip install requests-2.3.0.tar.gz

Install from local folder

tar -zxvf requests-2.3.0.tar.gz
cd requests-2.3.0
pip install .

You can delete the requests-2.3.0 folder.

Install from local folder (editable mode)

pip install -e .

This installs the package in editable mode. Any changes you make to the code will immediately apply across the system. This is useful if you are the package developer and want to test changes. It also means you can't delete the folder without breaking the install.

Elijah Sarver
  • 587
  • 6
  • 7
Colonel Panic
  • 119,181
  • 74
  • 363
  • 435
  • 12
    If you are used to using "python setup.py install" to install packages, it's natural to ask how to get "python setup.py install" to resolve dependencies with pip, but it's the wrong question. The solution is to install the package with pip and to stop using "setup.py install". – joeforker Jun 17 '15 at 20:00
  • 8
    The problem @joeforker is you might be forced to do use easy_install if you do something like `python setup.py test` – TomDotTom Mar 22 '17 at 19:42
  • 1
    @TomDotTom any idea on how to force to use pip when running `setup.py test`, please? – Jan Sila Feb 21 '18 at 06:55
  • @joeforker, pip uses `setup.py` behind the scenes. If I want people to be able to install my package with pip, I need to create a `setup.py` file. – cowlinator Oct 24 '19 at 00:14
  • If you want proof of this, try `pip install -e git+https://github.com/octocat/Hello-World.git#egg=Hello-World`. The error is `No such file or directory: 'c:\python\src\Hello-World\setup.py` – cowlinator Oct 24 '19 at 00:30
  • @cowlinator the trick is to always execute setup.py with pip and never run it on its own. However projects like https://flit.readthedocs.io/en/latest/ make it possible to put everything in pyproject.toml and have no setup.py at all. – joeforker Oct 24 '19 at 19:02
111

You can pip install a file perhaps by python setup.py sdist first. You can also pip install -e . which is like python setup.py develop.

Geoff Reedy
  • 31,995
  • 3
  • 51
  • 75
  • 14
    Just a heads up for anyone trying this: Note that `pip install -e` takes a directory as argument, not the `setup.py` file itself. At first I did not notice the period in `pip install -e .` which caused me some confusion :) – Markus Amalthea Magnuson Mar 04 '14 at 20:16
  • 3
    thanks, with `pip install -e .` I can install but with pip, how I can uninstall from editable mode ? – JuanPablo Jul 19 '14 at 15:14
  • 2
    Editable installs are uninstalled with a regular 'pip uninstall ' – joeforker Jun 17 '15 at 19:56
5

If you are really set on using python setup.py install you could try something like this:

from setuptools import setup, find_packages
from setuptools.command.install import install as InstallCommand


class Install(InstallCommand):
    """ Customized setuptools install command which uses pip. """

    def run(self, *args, **kwargs):
        import pip
        pip.main(['install', '.'])
        InstallCommand.run(self, *args, **kwargs)


setup(
    name='your_project',
    version='0.0.1a',
    cmdclass={
        'install': Install,
    },
    packages=find_packages(),
    install_requires=['simplejson']
)
TomDotTom
  • 4,901
  • 3
  • 32
  • 33
  • 1
    Thank you, but I am facing to recursive callback using this method. when I am using 'python setup.py install', it will call Install.run to call pip install ., this function will call Install. run again... – Lumen Aug 07 '17 at 21:16