20

We have CentOS with the ancient Python 2.4 interpreter.

But we would like to write out tests with a newer 2.5/2.6 syntax.

Assuming we have a second Python interpreter installed (e.g. python2.6) is there any way to run the 'nosetests' shell command and tell it to use a specific python interpreter instead of the default one?

Jacek Furmankiewicz
  • 1,143
  • 1
  • 13
  • 22

2 Answers2

21

The nosetests file is in Python, so it should just be a matter of running it in your new version. Find where the file is:

which nosetests

Then:

python2.6 /usr/bin/nosetests

Adjusting the name and path to match your system. I've not tested, but that should work.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • This is failing with an `ImportError`, for me… – Eric O. Lebigot Jan 16 '16 at 20:49
  • @EOL - an ImportError for nose, or for something else. You'll need to install nose inside the Python you're trying to run it with (and inside the virtualenv, if that's where you're trying to run it). – Thomas K Jan 17 '16 at 21:11
  • Yes, indeed. I was hoping that nosetests would adjust the module search path (if this makes sense). I don't see how the above answer solves the problem, then, since it seems that the nosetests from the question is installed inside the Python 2.4 interpreter and not the 2.6 one, so calling it with Python 2.6 should fail similarly… (?) In practice, it can be a bit difficult to install nose, sometimes: for example, I am using MacPorts, and both pip-2.4 and py24-nose are obsolete packages, so I would have to install nose manually. Why not. :) – Eric O. Lebigot Jan 18 '16 at 15:40
  • Yep, you do need the nose package installed for that version of Python, or at least accessible to it (putting it in the cwd would do). If you need a whole new Python environment, I'd recommend downloading [Miniconda](http://conda.pydata.org/miniconda.html) and starting from there. – Thomas K Jan 18 '16 at 23:21
4

Well, what I'd do is to install a different version of Python (2.6 say) and then create a virtualenv and install nose inside that. It will then use that version of nose and keep your stuff isolated.

You can also consider using tox to try to bridge interpreters.

Finally, you can simply run nose python2.6 $(which nose) [other options] args to run nose with the newer Python.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • [This answer](http://stackoverflow.com/a/13211456/1470262) to a related question quickly shows how to use virtualenvs. – Elaine Hale May 27 '14 at 23:01
  • Also, I am interested in your "Finally" comment, but cannot quite follow what you mean by it. – Elaine Hale May 27 '14 at 23:02
  • The actual `nose` command is a python script. The `$(which nose)` will expand to the full path of the script (this is virtualenv aware so you'll get the exact one you're running). Explicitly passing this as an argument to a preferred python interpreter will run the script with that interpreter. So, if you have `nose` installed somewhere with an older version of python in the `#!` line, you can use this to change the interpreter and run the script. – Noufal Ibrahim May 28 '14 at 15:44