How do I force `setup.py test` to install dependencies into my `virtualenv`?
Asked Answered
G

2

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?

Glide answered 7/3, 2012 at 19:10 Comment(2)
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.Varela
@Chris: not tests_requires dependencies.Jesus
T
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.

Tenant answered 8/1, 2014 at 18:6 Comment(11)
What then is the purpose of tests_requires ?Quicktempered
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.Tenant
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... https://mcmap.net/q/322805/-best-practices-how-do-you-list-required-dependencies-in-your-setup-pyQuicktempered
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.Tenant
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.Quicktempered
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".Tenant
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.Quicktempered
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.Tenant
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.Joejoeann
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.Tenant
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?Amling
H
-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='[email protected]',
    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).

Happy answered 11/5, 2012 at 15:5 Comment(3)
It doesn't use pip in my experience.Jesus
This is exactly what the OP did - ending up with .egg dirs all overCrystacrystal
This answer is explicitly incorrect. The doc states that setuptools will use EasyInstall to install the tests_require packages into "the project’s setup directory", whatever that might mean.Nominal

© 2022 - 2024 — McMap. All rights reserved.