33

In a crusade to make my application pip-installable, I'm fighting big fights with setuptools and distribute. I assume my dependencies are correct, i.e. installing with pip install myapp should probably fill the virtual environment correctly. However, I'd like to streamline development while I'm at it, so my goal is to start with an empty virtualenv and make setup.py test (and later setup.py develop, but that's a whole different fight) fill it with all defined dependencies.

And now to my problem: no matter how hard I try, all I get are dependencies installed as .eggs in my project directory which is sub-optimal at the very least. I tried creating a new setuptools command which would use pip (which seems to work, even though awkwardly) but that can't seriously be the solution (subclassing and overriding that is).

So how do I make setup.py test fill the virtualevn instead of my working directory?

Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
  • Can you add the output of your `python setup.py develop` command to the question? When I run that within a virtualenv, it installs dependencies to my virtualenv. – Chris Apr 28 '12 at 21:04
  • @Chris: not `tests_requires` dependencies. – Flimm Mar 25 '13 at 10:51

2 Answers2

13

By design, you can't make the tests_requires or the setup_requires entries go into the virtual environment. The idea is to separate what is required for performing tests/setup and what is required to actually use the package being installed. For example, I may require that the "coverage" module be needed for running tests on my package, but it isn't used by any of my code in the package. Therefore, if I didn't have "coverage" in my environment when I go and run tests, I wouldn't want "coverage" to get installed into the environment if my package didn't need it.

Ben Root
  • 610
  • 6
  • 12
  • 3
    What then is the purpose of tests_requires ? – Andy Hayden Nov 09 '14 at 06:18
  • 2
    In some cases, your tests may have an additional dependency that your package doesn't have in order to perform its tests. A particular example is the "nose" package, which may never get used anywhere else in your project but the tests. This allows you to ship just the package without tests, and not burden your users with unneeded dependcies to make the package work. – Ben Root Nov 19 '14 at 21:59
  • 6
    I meant the purpose of test_requires if it is only ever ignored... I had completely missed that it isn't ignored when doing `setup.py tests` / the venv behaviour seems surprising, I understand it shouldn't be default but you should be able to force pip to install of test_requires... http://stackoverflow.com/a/15422703/1240268 – Andy Hayden Nov 20 '14 at 00:19
  • I don't think the behavior is surprising, and I think my explanation makes it quite clear why it would be bad to do otherwise. However, you are free to file a feature request to force install of everything with the setuptools people, but I honestly don't see why that would be desirable. – Ben Root Nov 20 '14 at 16:03
  • 7
    Sorry, this doesn't explain why running `python setup.py test` wouldn't install the deps on the current venv. If you want to run the tests, it would be desirable to ensure you have all deps to run the tests. – Andy Hayden Nov 21 '14 at 03:11
  • Why should a venv be treated any differently from a stock environment? If you want test dependencies to always be installed, then put them under "install_requires". – Ben Root Nov 24 '14 at 20:16
  • 3
    That's the point it *is* being treated differently (`setup.py test` installs test_requires deps on stock). Anyway we're going round in circles. – Andy Hayden Nov 24 '14 at 20:24
  • No, it doesn't install it on stock (by install, I mean it gets placed into site-packages). The entire point of test_requires and setup_requires is to prevent contamination of your environment, whether it is stock or venv. – Ben Root Nov 24 '14 at 20:59
  • 3
    I would have thought the point of `tests_require` was to force installation of the test dependancies on `setup.py test` but not on `setup.py install` That, at least to me, seems what should be the desired behavior. – domoarigato Dec 24 '15 at 12:27
  • No, because you did not specify to install the test requirements. You only specified to run the tests. Therefore, running the tests should *not* trigger any actual installations, only to make available those requirements. – Ben Root Jan 07 '16 at 21:49
  • test dependencies should be installable into your venv for development purposes. the risk of accidentally bundling your development deps seems like fear mongering, respectfully. CI should be publishing most packages, not humans. not having dev deps in your python path is painful when you are trying to develop against libs that your editor cannot detect. code completion, API lookups, linters, etc--all lost when libs are missing from the PYTHONPATH. i am curious how pydevs workaround this. do some editors sniff or autoinstall your .eggs, and append the PYTHONPATH magically? – cdaringe Jan 11 '18 at 01:00
-6

If you are using setuptools, you can specify test dependencies using the tests_require keyword argument for the setup method.

from setuptools import setup

setup(
    name='your-package-name',
    version='1.0.0',
    author='me',
    author_email='me@mybasement.com',
    install_requires=['Pygments>=1.4'],
    tests_require=['nose'],
    packages=[
        'your_package_name',
    ],
)

When you run python setup.py test, this will check for nose and install it to the currently active virtualenv using pip if not already available.

Note that this argument will be ignored if you are using distribute.core.setup (nor will a test command be available).

Brian Gesiak
  • 6,648
  • 4
  • 35
  • 50
  • It doesn't use pip in my experience. – Flimm Mar 25 '13 at 10:50
  • This is exactly what the OP did - ending up with .egg dirs all over – UloPe Oct 13 '13 at 15:42
  • 5
    This answer is explicitly incorrect. The [doc](http://pythonhosted.org/setuptools/setuptools.html#new-and-changed-setup-keywords) states that setuptools will use EasyInstall to install the `tests_require` packages into "the project’s setup directory", whatever that might mean. – bukzor Jan 07 '14 at 22:55