我如何制定Pip尊重要求?

时间:2012-09-11 14:43:47

标签: python dependencies pip

如果我使用requires创建setup.py,则Pip不会安装我的依赖项。

这是我的setup.py:

from distutils.core import setup

setup(name='my_project',
      description="Just a test project",
      version="1.0",
      py_modules=['sample'],
      requires=['requests'])

我写了一个简单的sample.py:

import requests

def get_example():
    return requests.get("http://www.example.com")

然后我尝试安装它:

$ pip install -e .                                                                                    [15:39:10]
Obtaining file:///tmp/example_pip
  Running setup.py egg_info for package from file:///tmp/example_pip

Installing collected packages: my-project
  Running setup.py develop for my-project

    Creating /tmp/example_pip/my_venv/lib/python2.7/site-packages/my-project.egg-link (link to .)
    Adding my-project 1.0 to easy-install.pth file

    Installed /tmp/example_pip

请注意requests,我的依赖项未安装。如果我现在尝试使用我的测试项目:

$ python                                                                                              [15:35:40]
>>> import sample
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/example_pip/sample.py", line 1, in <module>
    import requests
ImportError: No module named requests

我做错了什么?

1 个答案:

答案 0 :(得分:15)

正确的拼写是install_requires,而不是requires;这确实需要您使用setuptools,而不是distutils

from setuptools import setup

setup(name='my_project',
      description="Just a test project",
      version="1.0",
      py_modules=['sample'],
      install_requires=['requests'])

我可以推荐Python Packaging User Guide来了解细节。