5

I usually develop with python 2.7, but would like to start creating some tools in python 3.x. What is the easiest way to have both running side-by-side, while keeping some semblance of control over what libraries I have installed where...

If I use pyenv to switch between versions, will it propagate to a generic shebang line? Something like

#!/usr/bin/env python

or even better, can I specify which python in the shebang?

#!/usr/bin/env python3

I am anticipating a lot of "Use virtualenv" replies. Is this really the only way to do it? I feel like I would like to have the "base" python on my system with whatever libraries I have installed so that I can change between the two environments by typing something simple like pyenv global 3.2.3

I am using OSX, Mountain Lion at the moment.


Trying to explain it a little better, I have two alternative questions:

  • If I use something like virtualenv, will I lose the ability to run python2 and python3 scripts alternately, without changing the environment (i.e., just via shebang)?

  • In contrast, if I use two independent version installations, how can I control/know what will be installed by pip or easy_install for example.


UPDATE: Currently using python3 in the shebang line, and using pip3 to install packages to python3... Seems to work fine.

beroe
  • 11,784
  • 5
  • 34
  • 79
  • What platform are you using? – Marcin Oct 19 '13 at 07:44
  • Thanks @Marcin. I added that I am using OSX. – beroe Oct 19 '13 at 07:46
  • This isn't helpful. You're asking a question. I came here to find an answer to that question. Now you "UPDATE" the question to remark that you've solved your real problem in a different way. Good for you, but the reason I'm here is that that other way is not an option for me. I want an answer to the original question. – reinierpost Apr 14 '21 at 16:08
  • ? @reinierpost, not sure what your issue is, but there is an attempted answer below. I updated my post more than a year after it had been originally asked, so.... I don't think that deterred people from answering. Great that you had the original question, and if you find something that works well, it would be helpful if you could provide that as an answer below. – beroe Apr 15 '21 at 00:19
  • Sorry, I was too hasty and interpreted your question incorrectly. – reinierpost Apr 15 '21 at 14:17

1 Answers1

3

You have a few possible methods varing slightly with os:

  1. Invoke python 2 as python and 3 as python3
  2. Extensions of .py and .py3
  3. Virtualenv
  4. A script or batch file to switch envionments. I use this to test my code against different versions of python 2. Note that you can have different envionments set in separate command sessions at the same time.
  5. Testing in virtual machines.
  6. Use an IDE that allows you to specify python versions on a project or debug session basis I use WingIDE but it is not the only one to allow this.

It may well pay you to look at six and working in python 3 than back converting to 2.7 for older installations.

See also here for both how to install python 3 libraries under python 3 with virtualevn and without, (modify version numbers as appropriate).

Update - the windows python launcher

Additionally, on Windows, when you install Python 3 you have the option to install the python launchers, (py.exe & pyw.exe), to the Windows directory. The py launcher has the following behaviour:

  • py Launch the python 3 interactive latest installed prompt (64 bit preferred)
  • py -2 Launch the python 2 interactive latest installed prompt (64 bit preferred)
  • py somescript *Check the shebang in the named file and launch it with python 2 (default if no shebang) or 3 as appropriate.
  • py -2 somescript launch the script under latest python 2 (64 bit preferred)
  • py -3 somescript launch the script under python 3 (64 bit preferred)
  • py -3.5 somescript launch the script under python 3.5 (64 bit preferred)
  • py -3.5-32 somescript launch the script under python 3.5 (32 bit)
  • the pyw launcher has the same behaviour without the console, like pythonw.

Associating .py files with py.exe and .pyw files with pyw.exe will result in the shebang line being honoured.

Additionally using py -3.5-32 -m pip install some_package will specifically install some_package to the 32 bit python 3.5 installation, etc.

Disclaimer: I am the author of some upcoming enhancements to the python launcher to extend the above options slightly.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Thanks Steve. If I just do two installs and use `python3` how do I control where things get installed? It was even a tribulation to update our Ubuntu machine from 2.6 to 2.7 because all the libraries were no longer found... – beroe Oct 19 '13 at 07:48
  • The second answer here http://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip gives you the answer to that. – Steve Barnes Oct 19 '13 at 08:03
  • 1
    N.B. You may be better off using your package manager to install python3-pip this will give you pip3. – Steve Barnes Oct 19 '13 at 08:22
  • 1
    Just to clarify at work I am using python 2.5.4, 2.6.6 & 2.7.3 plus python 3.3 to support various ages of project. – Steve Barnes Oct 19 '13 at 08:54