简体   繁体   中英

How to specify dependencies that setup.py needs during installation?

In some cases setup.py needs to import some extra modules, eg:

from setuptools import setup
import foo

setup(
    # e.g. a keyword uses `foo`
    version=foo.generate_version()
)

If foo is not installed, the execution of setup.py will fail because of ImportError .

I have tried to use setup_requires , eg setup_requires=['foo'] , but it doesn't help.

So how to specify this kind of dependencies?

Dependencies needed in setup.py cannot be specified in the very setup.py . You have to install them before running python setup.py :

pip install -r requirements.txt
python setup.py install

I have thought out a trick — call pip to install the dependencies in setup.py

import pip
pip.main(['install', 'foo', 'bar'])    # call pip to install them

# Now I can import foo & bar
import foo, bar
from setuptools import setup

setup(
    ...
)

pyproject.toml allows to specify custom build tooling:

[build-system]
requires = [..., foo, ...]
build-backend = "setuptools.build_meta"

I have similar problem, that I want to getting version of installer from yaml file, so I need to parse it and getting version from it.

I use following script as answer:

#!/usr/bin/env python

from setuptools import setup
import yaml


def find_version():
    with open('meta/main.yml') as meta_main:
        return yaml.load(meta_main)['version']


setup(name='sample',
      version=find_version(),
      packages=[],
      setup_requires=['pyyaml'])

I found that setup method has setup_requires parameter, that you can specify setup dependencies. As mentioned in setuptools doccumentation , projects listed in setup_requires will NOT be automatically installed on the system where the setup script is being run. If you want them to be installed, as well as being available when the setup script is run, you should add them to install_requires and setup_requires .

Let me know if you have any other problem.

you can add the dependencies with install_requires:

setup(
   ...
   install_requires=[
    'argparse',
    'setuptools==38.2.4',
    'docutils >= 0.3',
    'Django >= 1.11, != 1.11.1, <= 2',
    'requests[security, socks] >= 2.18.4',
   ],
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM